将div文本分配给变量然后显示它 [英] Assign div text to variable then show it

查看:175
本文介绍了将div文本分配给变量然后显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的任务,我试图完成学习JavaScript,但一直未能找到明确的答案。这是代码;

I have a simple task I'm trying to accomplish learning JavaScript but haven't been able to find a clear answer. Here's the code;

<script type="text/javascript">
var show = document.getElementById("box");
document.write(show);
</script>


<div id="box">Testing</div>

基本上我希望box div中的文本存储到一个变量中。然后,我想在页面的不同部分显示该变量的文本。使用上面的代码,我得到一个空错误。

Basically I want the text in the box div to be stored into a variable. Then, I want to display that variable's text on a different part of the page. With the code above I get a null error.

谢谢。

Thanks.

推荐答案

http://jsfiddle.net/David_Knowles/LTfyH/

<script>
    var show = document.getElementById("box").innerHTML;
    document.write(show);
</script>

这篇关于将div文本分配给变量然后显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆