简体   繁体   English

如何更改动态创建的div标签的内容?

[英]How to change the contents of dynamically created div tags?

My javascript function is as below. 我的JavaScript函数如下。 It gets all the parameter values. 它获取所有参数值。 But the function is not working. 但是该功能不起作用。 Please any suggestions? 有什么建议吗?

function getProductData(dealNo,title,url)
{
    // following line is not working
    document.getElementById(dealNo).innerHTML = "whatever";

}

edit: dealNo is the id of the dynamically created div tag 编辑:dealNo是动态创建的div标签的ID。

 document.getElementById('dealNo').innerHTML = "whatever";

Your code should be OK. 您的代码应该可以。 This is example from your code http://jsfiddle.net/4VNL7/ . 这是您的代码http://jsfiddle.net/4VNL7/中的示例。 Check is dealNo getting correct id and that id is already in HTML. 检查是否有dealNo获取正确的ID,并且该ID已存在于HTML中。 Maybe you are trying to modify div that is not in HTML. 也许您正在尝试修改不在HTML中的div

if you just want to add text to an existing div, try this document.getElementById('dealNo').innerText = "Hello" 如果您只想向现有的div添加文本,请尝试使用此document.getElementById('dealNo')。innerText =“ Hello”

of if you want to place an html to the existing div, try this document.getElementById('dealNo').innerHTML = "Hello" 如果您想将html放置到现有div,请尝试使用此document.getElementById('dealNo')。innerHTML =“ Hello”

if they are not working, see whether your div Id is indeed 'dealNo'. 如果它们不起作用,请查看您的div ID是否确实为'dealNo'。

You can also use jQuery to add to text div which are dynamically added. 您还可以使用jQuery将其添加到动态添加的文本div中。

HTML HTML

<div id="containerDiv"></div>

jQuery jQuery的

var divVariable = $('div');
$(divVariable).html('This is text in div');
$('#containerDiv').append(divVariable);

In above code I have created divVariable with div HTML instance, also appended text to that new created div in memory. 在上面的代码中,我使用div HTML实例创建了divVariable,还将文本附加到了内存中新创建的div。 Finally that div is appended to containerDiv (ie parent div). 最后,将该div附加到containerDiv(即父div)。

Dynamically div is child div. 动态div是子div。

Check this Fiddle Demo 检查此小提琴演示

Sorry,It was my fault. 对不起,这是我的错。 I haven't cleared the magento cache. 我还没有清除magento缓存。 That was the issue. 这就是问题所在。 Thank you all 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM