开发者

Select element based on condition using Javascript DOM

开发者 https://www.devze.com 2023-04-13 06:33 出处:网络
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

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

精彩评论

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