为什么我的脚本不起作用。我复制了jsfiddle.com中的代码,它可以工作,但在我的浏览器中没有。原因是什么?
<!doctype html>
<html>
<head>
<title>JavaScript test</title>
<script>
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "duplicetor" + ++i;
original.parentNode.appendChild(clone);
}
</script>
</head>
<body>
<button id="button" onlick="duplicate()">Click me</button>
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
</body>
</html>
答案 0 :(得分:1)
问题是你的javascript函数被加载到head中,在生成元素之前调用它。添加以下代码
window.addEventListener('load', function() {
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "duplicetor" + ++i;
original.parentNode.appendChild(clone);
}
});
<强>样本强>
<!doctype html>
<html>
<head>
<title>JavaScript test</title>
<script>
window.addEventListener('load', function() {
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "duplicetor" + ++i;
original.parentNode.appendChild(clone);
}
});
</script>
</head>
<body>
<button id="button" onlick="duplicate()">Click me</button>
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
</body>
</html>
&#13;
答案 1 :(得分:1)
脚本标记必须写在页面的末尾。
<html>
<head>
<title>JavaScript test</title>
<script>
</script>
</head>
<body>
<button id="button" onlick="duplicate()">Click me</button>
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
<script>
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "duplicetor" + ++i;
original.parentNode.appendChild(clone);
}
</script>
</body>
</html>
答案 2 :(得分:0)
我可以看到你在文档中读取dom元素之前提升和设置事件监听器(document.getElementById(&#39; button&#39;)。onclick = duplicate;)。这样做会引发错误。如果您运行脚本并在您尝试操作的元素之后提升,则它似乎有用。
<html>
<head>
<title>JavaScript test</title>
<script>
</script>
</head>
<body>
<button id="button" onlick="duplicate()">Click me</button>
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
<script>
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "duplicetor" + ++i;
original.parentNode.appendChild(clone);
}
</script>
</body>
</html>
&#13;
答案 3 :(得分:0)
在javascript函数上调用它之前,需要首先初始化该元素。所以只需在标记之前传输您的javascript。
<!doctype html>
<html>
<head>
<title>JavaScript test</title>
</head>
<body>
<button id="button" onlick="duplicate()">Click me</button>
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
<script>
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater');
function duplicate() {
var clone = original.cloneNode(true);
clone.id = "duplicetor" + ++i;
original.parentNode.appendChild(clone);
}
</script>
</body>
</html>
答案 4 :(得分:0)
您需要在正文部分
中添加脚本
<!doctype html>
<html>
<head>
<title>JavaScript test</title>
</head>
<body>
<button id="button" onlick="duplicate()">Click me</button>
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
<script type='text/javascript'>
</script>
</body>
</html>
const vector< vector< Blob< Dtype > * > > all_ tops = net_->top_vecs(); // get "top" of all layers
Blob<Dtype>* ptop = all_tops[layer_index][0]; // pointer to top blob of layer