开发者

How do I set the Id of a specific element in a class?

I have dynamically loaded content and I need to set the id开发者_高级运维 of a specific element. How do I do this?


Customize your dynamic rendering to have id attribute. If you don't have control over it, try accessing the DOM objects using some other ways like 'name','class','custom attribute'.

If you can not distinguish between your objects, then probably you are doing it wrong.

btw, if you want to do it desperately, you can do like below using jquery.

$(".myclass").attr('id','myID');


based on your question, you can assign an id with the following example:

// find the first span on the page and assign it an ID
var spans = document.getElementsByTagName('span');
if(spans.length > 0) {
  spans[0].id = 'your-id';
}

Most likey your business logic is more complicated then this. You'll need to traverse the dom using getElementsByTagName, etc to reach the desired node, then assign the id.

A quicker option would be to use a library such as jquery that allows you to quickly query nodes within the dom, and apply attributes:

$('your>business.logic').attr('id', 'your-id');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜