开发者

Select element based on condition using Javascript DOM

I have a jQuery statement as;

$("a[target="+iframeId+"]").attr("href", url);

What is the best way to write this in normal Javascript DOM such that 开发者_Go百科the perofrmance is not affected much?


One way to do it, though the jquery method is almost assuredly faster:

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
    if (links[i].getAttribute("target") == iframeId) {
        links[i].setAttribute("href", url);
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜