我有一个已由 jquery 加载的菜单。我不想编辑加载此菜单的当前 js 文件,因此我想使用 jquery 在该菜单中最后一个 li 的末尾添加一个额外的 li。我的菜单结构是:
<div class="toolbar">
<ul id="dcsns-filter" class="option-set filter">
<li><a href="#filter" data-group="dc-filter" data-filter="*" class="selected link-all">all</a>
</li>
</ul>
</div>
我尝试了下面的代码,但没有机会..
<script>
$(document).ready(function () {
var tsource = "http://www.google.com"
$(".dcsns-toolbar").find("li:last").after("<div style='width:100px; height:61px; float: left; top:0; position: absolute; margin-top:140px; margin-right:5px;'><div style=' width:65px; height:61px; float:left;'>Πηγή: <a href='" + tsource + "' target='_blank' style='color:#fff; text-decoration:none; font-family:Verdana, Geneva, sans-serif; font-size:12px;'>Google</a></div></div>");
});
</script>
谢谢!!
最佳答案
我相信您的 jQuery 选择器中有一个错误:
$(".dcsns-toolbar") // <--- This element doesn't exist
应该是
$('#dcsns-filter')
然后 jQuery 将找到您的元素,您可以将其附加到该元素上。
$('#dcsns-filter').find("li:last").after("<div style='width:100px; height:61px; float: left; top:0; position: absolute; margin-top:140px; margin-right:5px;'><div style=' width:65px; height:61px; float:left;'>Πηγή: <a href='" + tsource + "' target='_blank' style='color:#fff; text-decoration:none; font-family:Verdana, Geneva, sans-serif; font-size:12px;'>Google</a></div></div>");
关于javascript - 使用 jquery 在 ul 菜单中添加 li dynamik,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20613324/