我有这个 HTML:
<ul style="max-height: 400px;" class="dropdown-list-items current-list">
<li class="dropdown-list-item unselectable disabled " data-list-item-value="144" data-level-name="undefined" role="presentation">
<span class="list-item-description text-ellipsis">1 Week</span>
</li>
</ul>
我想向特定的 li
元素添加 title
属性。我在 ul
元素内的整个 HTML 中有许多 li
元素。我有这个 javascript/jquery:
$('.dropdown-list-item.unselectable.disabled option[data-list-item-value="144"]').attr('title', 'testing titles');
但这似乎不起作用。当我直接向 HTML 添加 title
时,它会出现在鼠标悬停时。但是,我不想将 title
直接写入 HTML。我的脚本格式错误或不正确吗?还是我还漏掉了其他东西?
最佳答案
您使用了错误的选择器,
$('.dropdown-list-item.unselectable.disabled[data-list-item-value="144"]').attr('title', 'testing titles');
https://jsfiddle.net/43mz84uk/3/
您的选择器尝试在 li
中查找 option
,您必须具有如下 HTML:
<li class="dropdown-list-item unselectable disabled">
<option data-list-item-value="144"><!-- << to this option you tried to set title -->
</li>
关于javascript - 向 li 元素添加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152721/