图像按钮串联变量给出未定义的错误

Image button concatenated variable gives undefined error

本文关键字:未定义 错误 变量 按钮 图像      更新时间:2023-09-26

我有一个不断增长的程序,其中我使用串联的html代码,变量整齐地放在我想要的地方,(下面几行):

var imginput =  "<input type='image' onclick='myFunction("
var imginput1 = ")' onmouseover='imgnw(this)' onmouseout='imgpic(this)' WIDTH='32' HEIGHT='32' BORDER='0' value='"

这(下面)是变量的串联,一切都很好,在我通过onclick事件得到的错误框的右边,我的变量整齐地嵌套在括号之间。

expressie = (imginput+tdid1+imginput1+tdid1+"' "+vectorwhite+afsluiter1 );

我得到的错误是一条未定义的消息,因为屏幕向量我需要函数onclick传递给function myFunction(tabloc)。。。tdid1=错误框中显示的变量,因此无论我点击哪个位置,字符串都在那里,但它从未到达myFunction(下面)

//function myFunction(tabloc) {
//(startloc == "0")
//  {startloc = tabloc;
//  }
//}

myFunction = function (tabloc){
    startloc = tabloc;
    d.write(startloc);
}

我同时尝试了两种版本,注释版本和脚本中的版本作为其他变体。问题是脚本的巨大性,我可以很容易地为这一部分编写一个单独的脚本,但我会偶然发现,我仍然需要旧脚本中的代码,它可以很好地工作。

那么,如何将我的变量转换为变量(startloc)???

要在Javascript代码中放入字符串,需要在其周围放置字符串分隔符(撇号或引号):

var imginput =  "<input type='image' onclick='myFunction('"";
var imginput1 = "'")' onmouseover='imgnw(this)' onmouseout='imgpic(this)' WIDTH='32' HEIGHT='32' BORDER='0' value='";

注意:这只会处理语法错误。如果字符串包含反斜杠或分隔符,则必须对其进行转义。此外,如果字符串包含字符<>&或属性分隔符(代码中的撇号),则需要对这些字符进行HTML编码。