使用jQuery更改HTML标记的属性

Changing the attribute of the HTML tag using jQuery

本文关键字:属性 HTML jQuery 更改 使用      更新时间:2023-09-26

我正试图使用jQuery更改<a>标记的属性,但它不起作用。我正在尝试将<a>标签的src属性从www.google.com更改为www.somewebsite.com。谁能告诉我哪里搞错了吗。

<p class="form-row text-varient">
    <label class="checkbox">
        Some text here
        <a target="_blank" href="http://google.com">Link Text</a>
    </label>
</p>
jQuery(document).ready(function(){
    jQuery(".text-varient").find("a").attr("href", "http://www.somewebsite.com/");
});
jQuery(document).ready(function(){
    $(".checkbox").find('a').attr("href", 'http://www.somewebsite.com');
 });

小提琴

试试这个:

<p class="form-row text-varient">
<label class="checkbox">
    Some text here
    <a target="_blank" href="http://google.com">Link Text</a>
</label>
</p>
<script>
$(document).ready(function(){
    $(".checkbox a").attr("href", "http://www.somewebsite.com");
});

</script>