我正在尝试将文本链接添加到带有“用户javascript和CSS”扩展名的youtube页面。但是以下js代码没有任何作用,我不确定Chrome控制台似乎对我的js没说什么。
$(window).load(function(){
var ytu = document.getElementsById('eow-title');
var newlink = document.createElement('a');
newlink.setAttribute('href',window.location.href);
newlink.setAttribute('innerHTML',document.title);
newlink.setAttribute('target','_blank');
$(newlink).insertAfter(ytu);
});
我已经选中了“ jquery2.1.0min”选项。
在您的代码中,加载[chrome]后没有执行$(window).load()
,这是由chrome造成的。因此,您必须使用$(document).ready()
方法来修改DOM。
另一件事是您写的document.getElementsById()
不正确,实际功能是document.getElementById()
。请检查以下代码段的代码。
$(document).ready(function(){
var ytu = document.getElementById('eow-title');
var newlink = document.createElement('a');
newlink.setAttribute('href',window.location.href);
newlink.setAttribute('innerHTML',document.title);
newlink.setAttribute('target','_blank');
newlink.innerHTML="Link";
$(newlink).insertAfter(ytu);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<div><div id="eow-title"></div></div>