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.
精彩评论