如何在不使用jquery的情况下使用javascript做.attr或类似的? [重复]

问题描述 投票:-4回答:3

这个问题在这里已有答案:

$("#my_iframe").attr("src","www.examples.com?x=asd&y=qwe");

这是使用jquery完成的,而我必须在没有jquery的情况下执行此操作。如何用javascript实现这个?

javascript jquery attr
3个回答
3
投票
document.getElementById('my_iframe').src = 'www.examples.com?x=asd&y=awe';

浏览器提供的DOM API为您提供了具有普通属性的对象。当然,它们不是“普通”属性,因为当你设置它们时会发生神奇的事情(其中一些)。

使用jQuery的“现代”版本,在这种情况下,使用.prop()而不是.attr()更好(一点点)。


1
投票
document.getElementById("my_iframe").src = "www.examples.com?x=asd&y=awe";

要么

document.getElementById("my_iframe").setAttribute("src","www.examples.com?x=asd&y=awe");

1
投票

您可以使用setAttribute()方法在特定元素上设置特定属性。喜欢

document.getElementById('my_iframe').setAttribute('src',  'www.examples.com?x=asd&y=awe');
© www.soinside.com 2019 - 2024. All rights reserved.