/*英语不好,抱歉*/
我正在尝试让新选项卡为我定制。此项目包含模式,此模式包含“制作按钮到Web”的功能。但是做完按钮后,附加的按钮就有问题了。这是我的代码。
var modal = document.getElementById("ID-modal");
var btnn = document.getElementsByClassName("goto-item")[0];
var span = document.getElementById("modal-cancel");
const modal_complete = document.getElementById("modal-complete");
var goto_content = document.querySelector(".goto-content");
const DEFAULT = "";
btnn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
function addBtn() {
var text_1 = document.getElementById("modal-text1").value;
var text_2 = document.getElementById("modal-text2").value;
if (text_1 == "" || text_2 == "") {
btn_off();
} else {
modal_complete.setAttribute('disabled', 'false');
var newBtn = document.createElement("input");
newBtn.type = "button";
newBtn.value = text_1;
goto_content.appendChild(newBtn);
newBtn.setAttribute("onclick", goWeb(text_2));
modal.style.display = "none";
}
}
function goWeb(text_2) {
location.href = text_2;
}<div class="goto">
<div class="goto-contain">
<div class="goto-content">
</div>
<div>
<input type="button" value="Add" class="goto-item" />
</div>
</div>
</div>
<div class="modal" id="ID-modal">
<div class="modal-content">
<p id="modal-baro">바로가기 추가</p>
<h7 class="modal-text3">이름</h7><br>
<input type="text" autocomplete="off" id="modal-text1"><br><br><br>
<h7 class="modal-text3">URL</h7><br>
<input type="text" autocomplete="off" id="modal-text2"><br><br><br>
<span class="modal-span" id="modal-delete">삭제</span>                 
<span class="modal-span" id="modal-cancel">취소</span> 
<span class="modal-span" id="modal-complete" onclick="addBtn();">완료</span>
</div>
</div>
</div>
因为我不知道哪里是问题所在,很抱歉糟糕的乱码。
goWeb或newBtn.setAttribute有问题吗?
发布于 2020-07-13 13:46:35
要使用setAttribute,您需要传递一个字符串:
newBtn.setAttribute("onclick", 'goWeb("' + text_2 + '")');但我只会使用[onclick][1]方法,这个方法就是为此而设计的,可以在所有浏览器上使用:
newBtn.onclick = function() {
goWeb(text_2)
}我们将函数封装在一个匿名函数中,以防止它立即被触发。
https://stackoverflow.com/questions/62868206
复制相似问题