2017-06-21 40 views
0
function close_w() { 
    //close the window 
} 
function imgopen(image) { 
    var newWindow=window.open("", "_blank", "width=450, height=400"); 
    var newButton=newWindow.document.createElement("button"); 
    var textNode=newWindow.document.createTextNode("Close"); 
    newButton.setAttribute("style", "text-align: center; width: 30%; height: 30px; margin-top: 10px; margin-left: 35%; border: none; color: #FFF; background-color: #DC143C"); 
    newButton.setAttribute("onclick", "close_w()"); 
    newWindow.document.newButton.appendChild(textNode); // Line Marker 
    newWindow.document.body.appendChild(newButton); 
} 

我想将一个按钮放在弹出窗口中,但将textNode附加到newButton不起作用,甚至按钮不显示。如果我删除标记的行,只显示按钮。关闭窗口并将文本节点追加到按钮

而且我还想让按钮被点击时弹出关闭,但我无法想象如何做到这一点。我可以使用什么函数来做close_W()?

这是一种任务:我不能在这里使用jQuery。

我对JS很虚弱。请帮忙!

回答

1

与其他按钮一样,您需要设置值newButton以获取添加到其中的文本。像newButton.setAttribute("value", "Close Window");。这个例子将用于input中的按钮。如果您要使用<button></button>标签,我建议您使用newButton.innerHTML = "Close Window";,因为应该将文本设置为“关闭窗口”。

+0

我在这里看到一些答案,说文本节点并将其附加到按钮将工作。有没有办法使用这种方式?我知道innerhtml会工作,但只是好奇。 – keist99

+0

我不知道如何能够使用文本节点并将其附加到按钮。向文本添加文本时,最好使用文本节点,例如'

',但我从未尝试在按钮上使用它。这可能是可能的,但我不知道你是如何做到的,但我仍然不会提出。 – CyanCoding

相关问题