如何在Javascript中动态引用字符串?
这有点可笑,我想为一个函数引用一个字符串值,但是javascript抛出了这个错误如何在Javascript中动态引用字符串?,javascript,Javascript,这有点可笑,我想为一个函数引用一个字符串值,但是javascript抛出了这个错误Uncaught SyntaxError:Unexpected token} 这是我的密码 var val = "Testing String"; var table_row = "<tr><td><a href='#' onclick='test('"+val+"')'>Row1</a></td></tr>"; function test(
Uncaught SyntaxError:Unexpected token}
这是我的密码
var val = "Testing String";
var table_row = "<tr><td><a href='#' onclick='test('"+val+"')'>Row1</a></td></tr>";
function test( val ){
alert( val );
}
测试函数中的值应为带引号的字符串
注意:如果我删除了val字符串的连接,并且pass是这样一个字符串
table_row = "<tr><td><a href='#' onClick='test(\"Testing String\")' >Row1</a></td>";
表_行=”;
。。。它起作用了
我在哪里闲逛
Gath.然后在原始脚本中也转义
“
:
我想这会对你有帮助的先生 如果第1行是变量 您可以通过
'+Row1+'
分隔符进行添加
或直接使用以下代码
var表_行=”;
//警报(“+table_row+”+;
见你,,,
var val = "Testing String";
var table_row = "<tr><td><a href='#' onclick='test('"+val+"')'>Row1</a></td></tr>";
所以onclick属性只是'test('
),因为'
结束了它
尝试:
var表_行=”“;
我就是这么做的,现在工作很好
var val = "Testing String";
table_row = "<tr><td><a href='#' onclick='test(\""+val+"\")'>Row1</a></td></tr>";
function test( val ){
alert( val) // Displays "Testing String"
}
var val=“测试字符串”;
表_row=“”;
功能测试(val){
警报(val)//显示“测试字符串”
}
感谢您的耐心。第1行是否真的有效?原始字符串(第一行)不包含任何要转义的引号:
var table_row = '<tr><td><a href="#" onClick="test(\'Testing String\')" >Row1</a></td>';
//alert("++++++++++++"+table_row+"++++++++++++");
var val = "Testing String";
var table_row = "<tr><td><a href='#' onclick='test('"+val+"')'>Row1</a></td></tr>";
"<tr><td><a href='#' onclick='test('Testing String')'>Row1</a></td></tr>"
var table_row = "<tr><td><a href='#' onclick=\"test('"+val+"')\">Row1</a></td></tr>";
var val = "Testing String";
table_row = "<tr><td><a href='#' onclick='test(\""+val+"\")'>Row1</a></td></tr>";
function test( val ){
alert( val) // Displays "Testing String"
}