I have this jQuery code that I am using to create a dynamic menu
function createList(test){
alert(test);
$('#nav')
.append('<li class="top"><a href="nogo2" id="products" class="top_link"><span class="down"></span></a></li>');
$('.down').text(test);
}
the problem that I have is when I try to add the text to the span
$('.down').text(test);
the menu changes to whatever value is the last for example if my values are abcde then all my menus eeeee can any body help me thanks
您可以尝试使用此选项仅选择最后一个.down而不是全部:
$('.down:last').text(test);
Why don't you do it directly like this:
function createList(test) {
alert(test);
$('#nav')
.append('<li class="top"><a href="nogo2" id="products" class="top_link"><span class="down">' + test + '</span></a></li>');
}
The selector $('.down')
select every elements with the class down
If you want to select the last created .down
use this :
$('.down:last').text(test);
$('.down').each(function(){
$(this).text(test);
});
i assume that you're calling createList() multiple times for each menu item, but this function creates a group of li's with the same class called "down". Then, you are modifying the class (ie all li's that have the class "down"). What you want is to assign a unique id to each li.
here is the fiddle solution:
http://jsfiddle.net/acturbo/vZAee/2/
this should work:
function createList(test){
//alert(test);
var id = "product-" + test;
console.log(test)
$('#nav')
.append('<li class="top"><a href="nogo2" id="products" class="top_link"><span id="'+ id +'"></span></a></li>');
$("#"+id).text(test);
}
$().ready(function() {
createList( "a");
createList( "b");
createList( "c");
});
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.