将内部HTML传递给Div [英] Passing Inner-Html to Div

查看:55
本文介绍了将内部HTML传递给Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在执行以下功能:

Hi to all.

i am making following function:

<pre lang="cs">function generatesciprtofwidget()
{
 //facebook like code
var facebooklike="you like facebookbutton";
document.getElementById(''getlikecode'').innerHTML = facebooklike; 
//getlikecode is a div which is located in body tag
alert(facebooklike);

return false;

 //facebook like code end
}




在正文标签中,我为div提供价值




in body tag i am giving value to div

<pre lang="xml"><div id="getlikecode">
    <script>
     alert(document.getElementById(''getlikecode'').value)
    </script>
    </div>






在单击按钮上,我调用了generateciprtofwidget()函数,这里是代码






On click button i call the generatesciprtofwidget() function here is code

<input id="Submit1" onclick="generatesciprtofwidget();"  type="submit" value="submit" />



现在的问题是警报,请显示不确定的原因,请帮忙!



now the problem is alert show undefine why please help!

推荐答案

可能是因为您使用document.getElementById(''getlikecode'')返回的HtmlElement没有名为value.您是通过写入innerHTML属性来进行设置的,因此也应像这样读回它:

Probably because the HtmlElement you''re returning with document.getElementById(''getlikecode'') doesn''t have a property called value. You''re setting it by writing to the innerHTML property, so read it back out like that too:

<div id="getlikecode">
    <script>
     alert(document.getElementById('getlikecode').innerHTML);
    </script>
    </div>


我认为问题在于将script放入正在讨论的div中.

想一想:您将脚本作为要用其他脚本修改的元素的内部HTML.我没感觉.
将脚本放在HTML head中(我会始终建议).元素div始终具有innerHTML(如果在HTML代码中为空,则可以将其设置为
).

Jim,关于value的观点是正确的,您不应将其用于div.您的修改代码正确.
I think the problem is putting script in the div in question.

Think of it: you''re putting the script as inner HTML of the element which you want to modify with other script. I don''t see a sense.
Put the script in the HTML head (I would recommend, always). The element div always have innerHTML (you can make if empty in your HTML code).

How, Jim is right about value, you should not use it for div. You modification code is correct.
document.getElementById('getlikecode').innerHTML = facebooklike;


我最近测试了这样的代码,它可以工作.

—SA


I recently tested such code, it works.

—SA


这篇关于将内部HTML传递给Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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