Nee*_*eel -1 jquery attributes list
我正在尝试使用以下代码填充ul
item = []
item.push('<li title ="head"' + 'id="' + innerValue + '">' + innerKey + " : " + innerValue + '</li>');
alert('<li title ="head"' + 'id="' + innerValue + '">' + innerKey +" : " + innerValue + '</li>');
alert($('[title="head"]').length);
Run Code Online (Sandbox Code Playgroud)
第一个警报给了我正确的价值.但第二个警报显示长度为0.
上面的代码有问题吗?
您最后的调用是搜索DOM,但您从未将这些元素添加到DOM中.如果您添加了它们,它们会显示在搜索上:
var item = [];
var innerValue = "value";
var innerKey = "key";
item.push('<li title ="head"' + 'id="' + innerValue + '">' + innerKey + " : " + innerValue + '</li>');
alert("Before adding to the DOM: " + $('[title="head"]').length);
$("#the-list").append(item[0]);
alert("After adding to the DOM: " + $('[title="head"]').length);Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="the-list"></ul>Run Code Online (Sandbox Code Playgroud)