На jQuery.данные, возвращающие неопределенные результаты
I'm trying to do is get a list of items from a node js server create li tags dynamically for each one, then somehow embed the ID that is coming from the server in each created HTML element as a custom attribute or an ID. I hope this make sense. Thanks in advanced for taking the time to help, I'm kinda new to this, so any help would be highly appreciated. What am I doing wrong? and is there another way to accomplish this? I included the code I tried below.
Что я уже пробовал:
//First getting thelist of items from the server items.forEach(function(item) { //adding the list to a variable to append later on to the html page var newItem= `<li>${item.name}</li>`; //I tried Jquery.data and .attr and both returns undefined $(newItem).data('id', item._id); //I also tried the vanilla javascript method: .setAttribute and I get an error that it's not a function. newItem.setAttribute('id', item._id); console.log($(item).data('id'));//undefined result
Ehsan Sajjad
можете ли вы войти в консоль newItem value и посмотреть, как она выглядит?
random_xyz
Большое вам спасибо за ваш ответ.
Он регистрирует HTML-элемент, созданный первым, но идентификатор пуст, вот скриншот: http://prntscr.com/i955u4