为什么调用这个 Javascript/jQuery 函数不起作用
我已将其放在页面顶部(但在 body
标记内)。
<script type="text/javascript">
function displayNumberBounds(numberElement, number){
alert("it works");
$(numberElement).before("<input type='text' size='6' id='lower' value='"+number+"' class='lt'/> < ");
$(numberElement).after(" > <input type='text' size='6' id='upper' value='"+number+"' class='gt'/>");
}
</script>
我还有这个(它是模板的一部分)
----编辑----
代码在页面上显示的方式是
..这里有一些JavaScript/HTML........
<script type="text/javascript">
function displayNumberBounds(numberElement, number){
alert("it works");
$(numberElement).before("<input type='text' size='6' id='lower' value='"+number+"' class='lt'/> < ");
$(numberElement).after(" > <input type='text' size='6' id='upper' value='"+number+"' class='gt'/>");
}
</script>
更多代码这里 ...
<script>
$(document).ready(function() {
$(".number-value").click(function() {
displayNumberBounds(this,0);
});
});
</script>
0
...循环迭代,从而给出第二个值...
<script>
$(document).ready(function() {
$(".number-value").click(function() {
displayNumberBounds(this,0);
});
}); 0
<script>
$(".number-value").click(function() {
displayNumberBounds(this,<%=number%>);
});
</script>
<span class="number-value" value="<%=number%>">
<%=number%>
</span>
不确定额外信息是否有帮助。
---------- 结束编辑 ---------
当我单击 <%=number%>
打印的值时,我得到了预期的结果适用于页面上的第一个 .number-value
项目,但不适用于后续项目。我不明白这一点。
I have put this at the top of my page (but within the body
tag).
<script type="text/javascript">
function displayNumberBounds(numberElement, number){
alert("it works");
$(numberElement).before("<input type='text' size='6' id='lower' value='"+number+"' class='lt'/> < ");
$(numberElement).after(" > <input type='text' size='6' id='upper' value='"+number+"' class='gt'/>");
}
</script>
and I have this further down (it's part of a template)
---- Edit ----
The way the code displays on the page is
.... some JavaScript/HTML here ....
<script type="text/javascript">
function displayNumberBounds(numberElement, number){
alert("it works");
$(numberElement).before("<input type='text' size='6' id='lower' value='"+number+"' class='lt'/> < ");
$(numberElement).after(" > <input type='text' size='6' id='upper' value='"+number+"' class='gt'/>");
}
</script>
.... more code here ...
<script>
$(document).ready(function() {
$(".number-value").click(function() {
displayNumberBounds(this,0);
});
});
</script>
0
... a loop iterates, thus giving the second value ...
<script>
$(document).ready(function() {
$(".number-value").click(function() {
displayNumberBounds(this,0);
});
});
0
<script>
$(".number-value").click(function() {
displayNumberBounds(this,<%=number%>);
});
</script>
<span class="number-value" value="<%=number%>">
<%=number%>
</span>
Not sure if the extra info helps.
---------- end edit ---------
When I click the value that is printed by <%=number%>
I get the expected result for the first .number-value
item that is on the page but not for subsequent ones. I don't understand this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在尝试在元素尚未加载时绑定单击元素。
你能试试这个吗?
查看此工作示例: http://jsfiddle.net/WS7Ha/
我还可以建议您移动为了可维护性,将所有 Javascript 保存到单独的 .js 文件中。
Looks like you're trying to bind the click element when the element isn't loaded yet.
Can you try this please.
Check out this working example: http://jsfiddle.net/WS7Ha/
May I also suggest to you to move all Javascript to a separate .js file for maintainability.
您重新绑定了很多次(
n
次),更好的方法是 数据属性,例如您的模板将如下所示:然后在页面顶部一次,绑定到所有这些属性,如下所示:
<一href="http://jsfiddle.net/nick_craver/GaDVK/" rel="nofollow">您可以在此处进行测试。
或者,它可能是无效的 HTML,但通过
.attr("value")
使用您的当前属性。 或者,如果文本始终匹配,您可以只使用$.trim($(this).text())
代替,不带任何属性。You're re-binding quite a bit, (
n
times), a better approach would be something like a data attribute, for example your template would look like this:Then once at the top of your page, bind to all of those, like this:
You can test it out here.
Or, it would be invalid HTML but use your current attribute, via
.attr("value")
. Or, if the text always matches up you could just use$.trim($(this).text())
instead, with no attribute.