我有以下 jQuery,当我单击按钮时,我用它来隐藏/显示 iFrame。
iFrame 包含一些 PHP,我只想在显示 iFrame 时加载它们。
$(document).ready(function()
{
$('#button').click(function()
{
$('#graphFrame').toggle().attr('src', 'graph1.php');
}
)
}
);
这工作正常,除了当我隐藏 iFrame 时它还会尝试加载 PHP。
有没有办法告诉它在我显示 iFrame(从而加载 php)时添加“src”属性,并在我隐藏 iFrame(从而不加载 PHP)时删除它?
最佳答案
尝试
$('#graphFrame').toggle(function(){
var $this = $(this);
$this.is(":visible") ? $this.attr('src', 'graph1.php') : $this.removeAttr('src')
});
关于jquery - 使用 jQuery 添加或删除属性来隐藏/显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16303881/