我有一个动态 Html表,其中有一个包含锚标记的单元格。除了锚标记之外,我还通过 OnClick 方法调用函数。该函数有两个参数,两者都是 String 类型。
其中,一个字符串包含类似" PUT请求| GET请求| JDBC请求|"
的值示例:
<td align='center'><a href='#' title='click here' onclick=showTestCaseReport('"
+ tcId + "','" + strStep + "')>Detail</a></td>
JavaScript功能:
function showTestCaseReport(testCaseId, testSteps)
{
alert(testCaseId);
alert(testSteps);
}
但是,当点击浏览器中的点击详细信息时,Javascript警报不会按照定义的功能显示。
已编辑经过分析,我发现上面提到的字符串值在中间有空格。删除像 PUTRequest | GETRequest | JDBCRequest | 这样的白色空格后,它正在工作。
问题:如果不删除字符串值中间的空格,有没有办法解决这个问题?
由于
答案 0 :(得分:0)
您的onclick属性缺少引号,请正确添加并继续 见https://jsbin.com/viqosobizo/edit?html,output
<td align='center'><a href='#' title='click here' onclick="showTestCaseReport('hello','great');">Detail</a></td>
答案 1 :(得分:0)
您没有展示如何创建该表。假设您的<td>
也包含在引号中,您需要转义它们或使用数据属性(推荐)
"<td align='center'><a href='#' title='click here'"+
" data-tcId='"+tcId+"' data-strStep='"+strStep+"'"+
" onclick='return showTestCaseReport(this)'>Detail</a></td>"
然后
function showTestCaseReport(cell) {
var testCaseId = cell.getAttribute("data-tcId"),
testSteps = cell.getAttribute("data-strStep");
....
return false; // cancel link
}