查找动态创建的div

发布于 2024-12-13 17:55:32 字数 777 浏览 0 评论 0原文

我想更改动态创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-12-20 17:55:32

现在很明显它是针对 Firefox 插件的:

document.getElementById 将在浏览器 UI 中搜索该元素,而不是网页。但稍后您将该元素添加到页面。因此,您必须在页面中搜索该元素:

var div = content.document.getElementById('windowx');

此外,您还进行了一些不必要的方法调用。这是代码的更清晰版本:

var doc = content.document,
    div = doc.getElementById('windowx'),
    btn;

if (div) {
  div.innerHTML = "something new"
}
else {
  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"))
}

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:

var div = content.document.getElementById('windowx');

Also, you are making some unnecessary method calls. Here is a cleaner version of your code:

var doc = content.document,
    div = doc.getElementById('windowx'),
    btn;

if (div) {
  div.innerHTML = "something new"
}
else {
  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"))
}
可可 2024-12-20 17:55:32

对我来说,它对 content.document 感到窒息,窗口对象没有内容属性,所以我不确定您在引用什么。清理了一下,似乎有效。请注意,我直接添加点击处理程序,而不是将其设置为属性。请参阅此处,我通过 jQuery 按钮单击处理程序触发了它: http://jsfiddle.net/hN8uz/1/

var doc = document;
var div = doc.getElementById('windowx');
var btn;

if (div) {
    div.innerHTML = "something new";
}
else {
    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.onclick = function() {
        document.body.removeChild(this.parentNode);
    };
    btn.appendChild(doc.createTextNode("Zamknij"));
}

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/

var doc = document;
var div = doc.getElementById('windowx');
var btn;

if (div) {
    div.innerHTML = "something new";
}
else {
    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.onclick = function() {
        document.body.removeChild(this.parentNode);
    };
    btn.appendChild(doc.createTextNode("Zamknij"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文