我遇到了一个意外错误,其中我的调试时间有点烦人。 我得到的错误是这样的:
未捕获的语法错误:意外的标记}
这是 HTML:
<html>
<head>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<form class="search" action="search" method="get">
<input type="text" id="search-string" />
<button type="button" onclick="return clearInputValue( $("#search-string") )">
<span class="clear-icon"></span>
</button>
</form>
<script>
var clearInputValue = function(a) {
// Ultimately I will check if the object passed to this function has a value greater than 1, and if so, I will clear the input
return false;
};
</script>
</body>
</html>
最佳答案
您需要使用一组不同的引号。
<button type="button" onclick="return clearInputValue( $('#search-string') )">
注意单引号。
此外,作为旁注,不鼓励使用内联 JavaScript。最好将内容 (html) 与行为 (javascript) 分开,就像处理演示文稿 (css) 一样。
关于javascript - 为什么我不能将 jquery 选择器对象从内联 HTML 元素传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791743/