gpt4 book ai didi

jquery 中的 javascript 切换未按预期工作

转载 作者:行者123 更新时间:2023-11-28 18:13:45 25 4
gpt4 key购买 nike

我有一个按钮,只需使用 JavaScript 即可打开和关闭 iframe。

但是当我单击该按钮时,该按钮不仅会消失,还会与 iframe 一起消失。

在检查 Firebug 控制台时,按钮的 CSS 还将显示参数更改为“none”。

$(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").toggle('show');

它会切换页面中的所有 div。这包括包含按钮的那个。您可以向要隐藏的 div 添加 id 以使其正常工作:

var div = document.createElement("div");
div.id = "to-toggle";
...
setTimeout(function() {
$("#button2").css({"position": "fixed", "display": "inline", "bottom": "0", "right": "0"}).click(function(){
$("#to-toggle").toggle('show');
});
}, 100);

关于jquery 中的 javascript 切换未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41091872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com