所以你有一堆元素,你用 jQuery 选择其中一个。
($('div > ul > li').length === 1) // this is true
现在,如果您需要将此元素作为字符串传递,您通常会:
var el = $('div > ul > lu');
var passid = el.prop('id');
// in case this element doesn't have an id="..." attribute set one
if(typeof(passid)!=='string'){
// compute an id="..." attribute based on the time
var elindex = new Date().getTime();
// make sure the id we're using isn't already in use by a different element
while($('#el_'+elindex).length)
elindex++;
// assign what we've got
passid = 'el_'+ elindex ;
el.prop('id', passid);
};
passid = '#'+passid;
// Once we're good to go, pass the resulting selector
// passon(passid);... etc.
jQuery 是否带有更好的(内置)函数(甚至是插件)来方便我将 DOM 节点的引用作为字符串传递?