我有以下div,并且要更改显示“没有帐户?”的文字去别的东西。

我试过$('a#createAccount').text'some text).html('some text');
$('.create p')相同,但会删除<a>

<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>
</div>


仅更改保持<a>不变的文本

最佳答案

您可以选择锚标记,将其放入变量中,覆盖段落的内容,然后将锚标记附加回它。



let anchor = $('a#createAccount');
let paragraph = $('.create p')
paragraph.text("New text ");
paragraph.append(anchor);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>
</div>

07-24 09:51
查看更多