Tag: attr

prop()和attr()在jQuery和何时使用attr()和prop()的区别

我在一些地方看到.attr()用在jQuery中。在一些地方.prop()被使用。但是我在SOsearch和谷歌我很困惑。请告诉我这两者之间的确切区别,以及何时使用它们。 我已经看到以下链接jQuery attr与道具,有一个道具列表? jQuery attr vs道具? 但是我没有得到答案,请帮助我。提前感谢。 在给出一个downvote之前,请解释原因,然后我会纠正我的下一个职位。

如何通过javascript来改变button上的<a>标签的href

如何通过点击button来改变一个<a/>标签的href属性值? <script type="text/javascript"> function f1() { document.getElementById("abc").href="xyz.php"; } </script> <a href="" id="abc">jhg</a> <a href="" id="" onclick="f1()">jhhghj</a>

.prop()vs .attr()

所以jQuery 1.6有了新的函数prop() 。 $(selector).click(function(){ //instead of: this.getAttribute('style'); //do i use: $(this).prop('style'); //or: $(this).attr('style'); }) 或在这种情况下他们做同样的事情? 如果我必须切换到使用prop() ,所有旧的attr()调用将打破,如果我切换到1.6? UPDATE selector = '#id' $(selector).click(function() { //instead of: var getAtt = this.getAttribute('style'); //do i use: var thisProp = $(this).prop('style'); //or: var thisAttr = $(this).attr('style'); console.log(getAtt, thisProp, thisAttr); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script> <div id='id' style="color: red;background: orange;">test</div> (另见这个小提琴: http : […]