0

尝试使用 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>
4

1 回答 1

3

Replace this:

var $this = $('this'),

with this:

var $this = $(this),   // No need of any single quotes here for this keyword
于 2013-06-15T14:52:11.503 回答