jQuery在控制台中工作,但不在原始代码中 [英] jQuery working in console but not in original code

查看:61
本文介绍了jQuery在控制台中工作,但不在原始代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码将href添加到td内的a标签中.当我在控制台中运行时,它工作正常.但是,当我在我的代码中尝试同样的方法时,它不起作用.谁能告诉我原因?

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?

<script>
    $("table tbody tr td a").attr('href','http://www.google.com');
 </script>
<table>
    <tr>
        <td><a >Hai</a></td>
    </tr>
</table>

推荐答案

使用document.Ready()

Use document.Ready()

$(document).ready(function() {
    $("table tbody tr td a").attr('href','http://www.google.com');
});

在尝试操作DOM之前,您需要确保已加载文档.

You need to ensure that the document is already loaded before you try to manipulate the DOM.

更多信息: http://api.jquery.com/ready/

这篇关于jQuery在控制台中工作,但不在原始代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆