开发者

Using attr on elements created with javascript

If I crea开发者_如何学Gote a new DOM element with Javascript document.createElement, is there a way to use jquery function attr() to change the element's attribute?


var element = document.createElement(tagName);
$(element).attr('foo', 'bar');


you can use javascript like :

var link = document.createElement('a');
link.setAttribute('href', 'mypage.htm');

or jquery

$(link).attr('href', 'mypage.htm');


Of course you can, but compare:

var element = document.createElement('input');

$(element).attr('type', 'button');

// vs.

element.type = 'button';

IMO I would simply use the second approach, staying away from the IE's buggy element.setAttribute method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜