简体   繁体   中英

Adding a li dynamik in a ul menu with jquery

I have a menu that is already loaded by jquery. I dont want to edit the current js file tha loads this menu so i want with jquery to add an extra li at the end of the last li in this menu. My menu structure is:

<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>    

I tried the code below without chance..

<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>

Thank you!!

I beleive you have an error in your jQuery Selector:

$(".dcsns-toolbar")  // <--- This element doesn't exist

should be

$('#dcsns-filter')

and then jQuery will find your element and you can append to that.

$('#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>");
var innerHtml = '<div>...</div>';
var newListItem = $('<li />').html(innerHtml);
$("#dcsns-filter").append(newListItem);
$("#dcsns-filter").append('Your new li html');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM