jquery创建一个唯一的id [英] jquery create a unique id
本文介绍了jquery创建一个唯一的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$(document).ready(function() {
$('a.menuitem').click(function() {
var arr = 0;
var link = $( this ), url = link.attr( "href" );
var newDiv = $( document.createElement( 'div' ) )
$( "#content_pane" ).append( newDiv );
newDiv.load( url );
return false;
});
});
如你所见,我正在创建 div
并添加一些内容,我如何给每个 div
创建一个唯一的id,比如section1,section2,section3等?
As you can see I am creating a div
and adding some content to it, how would I give each div
that is created a unique id, something like section1, section2, section3, etc?
推荐答案
只需使用计数器:
var section = 1;
$(function() {
$("a.menuitem").click(function() {
...
$("<div></div>").attr("id", "section" + section++).appendTo("#content_pane");
...
return false;
});
});
此外,我建议按上述方式创建元素。
Also, I'd suggest creating the element as per above.
这篇关于jquery创建一个唯一的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文