我有一個按鈕,使用JavaScript來打開和關閉iframe。在jQuery中的javascript切換不按預期工作
但是,當我點擊按鈕,而不是隻是iframe消失,按鈕也隨着iframe消失。
在檢查螢幕控制檯時,按鈕的CSS也會將顯示參數更改爲'無'。
$(document).ready(function(){
var iDiv = document.createElement('div');
iDiv.id = 'enigma';
iDiv.setAttribute("style", "font-size:18px;position:fixed;bottom:20px;right:5px;display:inline");
document.body.appendChild(iDiv);
var div = document.createElement("div");
div.innerHTML = '<iframe src="http://example.com/index.html" width="400" height="200" style="border:0"></iframe>';
iDiv.appendChild(div);
var div2 = document.createElement("div");
div2.setAttribute("style","display:inline;");
div2.innerHTML = '<button id="button2">Show/Hide</button>';
document.body.appendChild(div2);
setTimeout(function() {
$("#button2").css({"position": "fixed", "display": "inline", "bottom": "0", "right": "0"}).click(function(){
$("div").toggle('show');
});
}, 100);
});
那麼當你點擊按鈕時,包括那個按鈕在內,你切換所有'div'。 – Iluvatar
您正在切換每個「div」。你的按鈕是'div'嗎?請包括標記。 – Carcigenicate
啊......我的壞...我得到了我做錯了什麼。我如何最好地改變這隻切換iframe?我應該添加iframe ID嗎? – hnvasa