查找动态创建的div
我想更改动态创建的 div 但我无法让它工作 它不断创建新的 div
var div = document.getElementById('windowx');
var btn;
var doc = content.document
if (div)
{
div = document.getElementById('windowx')
div.innerHTML = "something new"
}
else
{
doc = content.document
div = doc.body.appendChild(doc.createElement("div"))
div.setAttribute("id", "windowx")
div.setAttribute("style",
"position: fixed; top: 100px; left: 100px; width: 20em;"
+ "border: 2px outset orange; background-color: cornsilk;"
)
btn = div.appendChild(doc.createElement("button"))
btn.setAttribute("style", "position: absolute; bottom: 1ex; right: 1ex;")
btn.setAttribute("onclick", "document.body.removeChild(this.parentNode)")
btn.appendChild(doc.createTextNode("Zamknij"))
}
I want to change my dynamically created div
but i can't get this working
it's keep creating new divs
var div = document.getElementById('windowx');
var btn;
var doc = content.document
if (div)
{
div = document.getElementById('windowx')
div.innerHTML = "something new"
}
else
{
doc = content.document
div = doc.body.appendChild(doc.createElement("div"))
div.setAttribute("id", "windowx")
div.setAttribute("style",
"position: fixed; top: 100px; left: 100px; width: 20em;"
+ "border: 2px outset orange; background-color: cornsilk;"
)
btn = div.appendChild(doc.createElement("button"))
btn.setAttribute("style", "position: absolute; bottom: 1ex; right: 1ex;")
btn.setAttribute("onclick", "document.body.removeChild(this.parentNode)")
btn.appendChild(doc.createTextNode("Zamknij"))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在很明显它是针对 Firefox 插件的:
document.getElementById
将在浏览器 UI 中搜索该元素,而不是网页。但稍后您将该元素添加到页面。因此,您必须在页面中搜索该元素:此外,您还进行了一些不必要的方法调用。这是代码的更清晰版本:
Now that it is clear that it is for a Firefox plugin:
document.getElementById
will search for that element in the browser UI, not the web page. But later you are adding the element to the page. Therefore you have to search in the page for that element:Also, you are making some unnecessary method calls. Here is a cleaner version of your code:
对我来说,它对
content.document
感到窒息,窗口对象没有内容属性,所以我不确定您在引用什么。清理了一下,似乎有效。请注意,我直接添加点击处理程序,而不是将其设置为属性。请参阅此处,我通过 jQuery 按钮单击处理程序触发了它: http://jsfiddle.net/hN8uz/1/For me it was choking on
content.document
, the window object doesn't have a content property so I'm not sure what you were referencing. Cleaned up a bit and it seems to work. Note I'm adding the click handler directly instead of setting it as an attribute. See here where I have it triggered by a jQuery button click handler: http://jsfiddle.net/hN8uz/1/