jquery - <link> 上的 attr() 没有生效

标签 jquery html css dom

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

相关文章:

javascript - 根据页面位置更改类

html - 在居中列表中居中元素符号

javascript - 网站页面加载器持续时间问题

html - 背景图像溢出不起作用

html - 重叠问题

javascript - 桌面 View 显示文本但在移动 View 中文本不显示

javascript - 验证用户提交的位置

jquery - Colorbox 从 iFrame 加载特定 div

javascript - Vuejs 和 Bootstrap。有条件地禁用按钮组中的按钮

html - 右侧 div 可变宽度,左侧 div 占用剩余宽度