尝试使用attr()更改属性时,我的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>
答案 0 :(得分:3)
替换它:
var $this = $('this'),
用这个:
var $this = $(this), // No need of any single quotes here for this keyword