jQuery 在控制台中工作但不在原始代码中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17660229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery working in console but not in original code
提问by Midhun Mathew
I have tried the following code to add href to the a tag inside a td. it is working fine while i do in console. But when I try the same in my code it is not working. Can anyone tell me the reason?
我已尝试使用以下代码将 href 添加到 td 中的 a 标记。当我在控制台中时它工作正常。但是当我在我的代码中尝试相同时,它不起作用。谁能告诉我原因?
<script>
$("table tbody tr td a").attr('href','http://www.google.com');
</script>
<table>
<tr>
<td><a >Hai</a></td>
</tr>
</table>
回答by Guilherme de Jesus Santos
Use document.Ready()
使用 document.Ready()
$(document).ready(function() {
$("table tbody tr td a").attr('href','http://www.google.com');
});
You need to ensure that the document is already loaded before you try to manipulate the DOM.
在尝试操作 DOM 之前,您需要确保文档已经加载。
More info: http://api.jquery.com/ready/
更多信息:http: //api.jquery.com/ready/
回答by Jeremy Gallant
The element doesn't exist when your jquery is executing. You need to put your handlers inside a ready function.
当您的 jquery 正在执行时,该元素不存在。您需要将处理程序放在就绪函数中。
<script type="text/javascript">
$(function() {
$("table tbody tr td a").attr('href','http://www.google.com');
});
</script>
$(function() {});
is the shorthand for $(document).ready(function() {});
$(function() {});
是简写 $(document).ready(function() {});
回答by Stefan
The JS is firing before the html is created.
JS 在创建 html 之前触发。
<table>
<tr>
<td><a >Hai</a></td>
</tr>
</table>
<script>
$(function() {
$("table tbody tr td a").attr('href','http://www.google.com');
});
</script>
回答by user
put it in a ready section :
把它放在一个准备好的部分:
<script type="text/javascript">
$(document).ready(function() {
$("table tbody tr td a").attr('href','http://www.google.com');
});
</script>
回答by DarkAjax
Your code executes before the DOM is ready and the element actually exists, try it this way:
您的代码在 DOM 准备好并且元素实际存在之前执行,请尝试以下方式:
<script>
$(document).ready(function(){
$("table tbody tr td a").attr('href','http://www.google.com');
});
</script>
The reason it works on console is because the <a>
element already exists when you execute your code...
它在控制台上工作的原因是因为<a>
执行代码时该元素已经存在......
回答by Jaggan_j
The order of your jQuery files in the master layout page can also influence why it does not work in actual code but works in console of the browser. jQuery files must be referenced in the following order in the master page:
主布局页面中 jQuery 文件的顺序也会影响它为什么在实际代码中不起作用但在浏览器的控制台中起作用。必须在母版页中按以下顺序引用 jQuery 文件:
<script type="text/javascript" src="/Scripts/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery-ui-1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="/Scripts/bootstrap.min.js"></script>
For people coming here looking for a solution to what it says in title without going into specifics of this particular question, hope it helps someone.
对于来这里寻找标题中所说内容的解决方案的人,没有深入了解这个特定问题的细节,希望它可以帮助某人。