尝试使用 attr() 更改 a 的属性时,我的 jQuery 代码无法正常工作,href 设置为 null,而不是白天或晚上 这是我的代码,有谁知道我该如何修复它?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="day.css">
<h1>This is my website</h1>
<button data-file="day">Day</button>
<button data-file="night">Night</button>
<script>
var link = $('link');
(function(){
$('button').on('click',function(){
var $this = $('this'),
stylesheet = $this.data('file');
$this.siblings('button').removeAttr('disabled');
link.attr('href', stylesheet + '.css');
$this.attr('disabled', 'disabled');
});
})();
</script>
最佳答案
替换这个:
var $this = $('this'),
用这个:
var $this = $(this), // No need of any single quotes here for this keyword
关于jquery - <link> 上的 attr() 没有生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17124849/