OnClick我想要一个对话框按钮弹出,对话框是一个uniqueId并且里面有一个iframe。这是我的代码:
$( ".insertDetailsDiv" ).click(function() {
var idName = $(this).attr("id");
var idNameBase = idName.replace('DetailsButton', '');
var iframeSRC = idNameBase+".php";
$("<div class='idNameDialogDivClass'></div>").dialog();
$(".idNameDialogDivClass").each(function() {
$(this).uniqueId();
var idNameDialogDiv = $(this).attr("id");
console.log(idNameDialogDiv);
});
$("<iframe></iframe>", {
id: idNameBase,
src: iframeSRC
}).appendTo('#'+idNameDialogDiv);
});
但是我的控制台日志(chrome)输出
ui-id-6
Uncaught ReferenceError: idNameDialogDiv is not defined
我不明白,控制台日志输出变量,然后javascript没有识别变量。
答案 0 :(得分:1)
尝试在idNameDialogDiv
函数
.each()
$( ".insertDetailsDiv" ).click(function() {
var idName = $(this).attr("id");
var idNameBase = idName.replace('DetailsButton', '');
var idNameDialogDiv = null; // Declare here.
var iframeSRC = idNameBase+".php";
$("<div class='idNameDialogDivClass'></div>").dialog();
$(".idNameDialogDivClass").each(function() {
$(this).uniqueId();
idNameDialogDiv = $(this).attr("id");
console.log(idNameDialogDiv);
});
$("<iframe></iframe>", {
id: idNameBase,
src: iframeSRC
}).appendTo('#'+idNameDialogDiv); // This was the place where the error was caused
});
答案 1 :(得分:0)
idNameDialogDiv是您在$.each
函数范围内定义的变量。它不在此块之外
$(".idNameDialogDivClass").each(function() { // <- function declaration
$(this).uniqueId();
var idNameDialogDiv = $(this).attr("id"); // <- variable declaration
console.log(idNameDialogDiv);
});
您需要在函数外部移动此变量才能访问您当前所在的位置