Javascript 将span的内容添加到div中,并使其在单击按钮时可见
当用户在我的页面上输入Javascript 将span的内容添加到div中,并使其在单击按钮时可见,javascript,Javascript,当用户在我的页面上输入url时,我正在输入一些内容。 我想,当用户单击按钮时,内容应设置为,然后显示div 这是我的小提琴: 试试这个: <body> <div height="40" style="display:none" width="50" border="2" id="d1"> </div> <span id="s1">This should be included into into div on button click</sp
url
时,我正在输入一些内容。
我想,当用户单击按钮时,内容应设置为
,然后显示div
这是我的小提琴:
试试这个:
<body>
<div height="40" style="display:none" width="50" border="2" id="d1">
</div>
<span id="s1">This should be included into into div on button click</span>
<button type="submit" onclick="myfunc();"> Button</button>
</body>
<script>
function myfunc() {
var html = document.getElementById("s1").innerHTML;
document.getElementById("d1").style.display = "block";
document.getElementById("d1").innerHTML = html;
}
</script>
单击按钮时,应将其包含在div中
按钮
函数myfunc(){
var html=document.getElementById(“s1”).innerHTML;
document.getElementById(“d1”).style.display=“block”;
document.getElementById(“d1”).innerHTML=html;
}
你的小提琴有一些打字错误。以下是修改后的版本:
HTML:
顺便问一下,如果可以的话,为什么不直接使用jquery呢
以下是在jquery中执行此操作的一种方法:
HTML:
谢谢你的回答,但没有变化。请看:在我的电脑上运行良好。。您能检查一下您的控制台吗?我欢迎jquery,但我不太赞同它。如果您能添加并让我知道jquery的良好参考,我将不胜感激,因为它显示了基本示例@xaisoft innerText不兼容跨浏览器。它在Mozilla中不起作用嘿,它在你的小提琴中起作用。我在这里使用了相同的脚本,但有什么问题吗@shadow:我有经验,你能给出解决方案吗?@Programming_疯狂使用document.getElementById('s1')。innerText | | document.getElementById('s1')。textContent
<div style="border:1px solid black;height:40px;display:none;" id="d1">
</div>
<span id="s1">This should be included into into div on button click</span>
<button type="submit" onclick="myfunc()">Button</button>
function myfunc()
{
document.getElementById('d1').innerText=document.getElementById('s1').innerText;
document.getElementById('d1').style.display='block';
}
<div id="d1">
</div>
<span id="s1">This should be included into into div on button click</span>
<button id="btn" type="submit">Button</button>
$('#btn').click(function()
{
myfunc();
});
function myfunc()
{
var div = $('#d1');
var spanText = $('#s1').text();
div.text(spanText);
d1.show();
}