Javascript 无法读取属性';增补列表器';copycode.js中的空值
单击“检查元素”时,复制剪贴板工具提示出现错误: 无法读取null的属性“addEventListener” 下面是我的JS代码:Javascript 无法读取属性';增补列表器';copycode.js中的空值,javascript,jquery,html,Javascript,Jquery,Html,单击“检查元素”时,复制剪贴板工具提示出现错误: 无法读取null的属性“addEventListener” 下面是我的JS代码: createtooltip() var buddhaquote = document.getElementById('mytext') buddhaquote.addEventListener('mouseup', function(e){ var selected = getSelectionText() // call getSelectionText(
createtooltip()
var buddhaquote = document.getElementById('mytext')
buddhaquote.addEventListener('mouseup', function(e){
var selected = getSelectionText() // call getSelectionText() to see what was selected
if (selected.length > 0){ // if selected text length is greater than 0
var copysuccess = copySelectionText() // copy user selected text to clipboard
showtooltip(e)
}
}, false)
错误
无法读取null的属性“XXX”
表示您正在使用包含null
的变量。如果您实际尝试调试程序并使用了console.log
,您将看到变量buddhaquote
包含null而不是所需的元素
这可能有两个原因,很难说,因为您决定不共享足够的代码
mytext
的HTML元素不存在<script>
var test = document.getElementById("test");
// Error: Cannot read property 'appendChild' of null.
test.appendChild(new Text("Hello world!"));
</script>
<!-- only after this you can access span#test -->
<span id="test"></span>
var测试=document.getElementById(“测试”);
//错误:无法读取null的属性“appendChild”。
appendChild(新文本(“helloworld!”);
<span id="test"></span>
<script>
var test = document.getElementById("test");
test.appendChild(new Text("Hello world!"));
</script>
var测试=document.getElementById(“测试”);
appendChild(新文本(“helloworld!”);
尝试将代码导入
window.onload=function(){ --your code -- }
并检查您是否没有尝试获取类而不是id在
控制台中打印budhaquote
变量。log
?或者只是在控制台中运行document.getElementById('mytext')
?