将内部HTML传递给Div [英] Passing Inner-Html to Div
本文介绍了将内部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 withdocument.getElementById(''getlikecode'')
doesn''t have a property calledvalue
. You''re setting it by writing to theinnerHTML
property, so read it back out like that too:
<div id="getlikecode">
<script>
alert(document.getElementById('getlikecode').innerHTML);
</script>
</div>
我认为问题在于将script
放入正在讨论的div
中.
想一想:您将脚本作为要用其他脚本修改的元素的内部HTML.我没感觉.
将脚本放在HTMLhead
中(我会始终建议).元素div
始终具有innerHTML
(如果在HTML代码中为空,则可以将其设置为
).
Jim,关于value
的观点是正确的,您不应将其用于div
.您的修改代码正确.
I think the problem is puttingscript
in thediv
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 HTMLhead
(I would recommend, always). The elementdiv
always haveinnerHTML
(you can make if empty in your HTML code).
How, Jim is right aboutvalue
, you should not use it fordiv
. You modification code is correct.
document.getElementById('getlikecode').innerHTML = facebooklike;
我最近测试了这样的代码,它可以工作.
I recently tested such code, it works.
这篇关于将内部HTML传递给Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文