简体   繁体   English

如何在不影响其子元素的情况下更改元素的文本?

[英]How can I change the text of an element without affecting its child element?

I have the following div and want to change the text that says "Don't have an account?"我有以下 div 并想更改“没有帐户?”的文本。 to something else.到别的东西。

I've tried $('a#createAccount').text'some text) or .html('some text');我试过$('a#createAccount').text'some text).html('some text'); Same with $('.create p') , but it removes the <a> .$('.create p')相同,但它删除了<a>

<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="https://somewebsite.com">Sign up now</a>
  </p>
</div>

change only the text leaving the <a> unchanged仅更改保持<a>不变的文本

You can select the anchor tag, put it in a variable, overwrite the content of your paragraph and then append the anchor tag back to it. 您可以选择锚标记,将其放入变量中,覆盖段落的内容,然后将锚标记附加回它。

 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> 

Per How do I select text nodes with jQuery? 我如何使用jQuery选择文本节点? , you can leverage chained functions to filter your matched elements to the text nodes within a parent element, then set their data attribute to the text that you want: ,您可以利用链接函数将匹配的元素过滤到父元素内的文本节点,然后将其data属性设置为所需的文本:

 $(document).ready(function() { $(".create p").contents().filter(function() { return this.nodeType === 3; //Node.TEXT_NODE }).each(function() { if ($(this).text().trim() !== "") this.data = 'some text'; // ensure that stray blank text nodes aren't caught up in this change }); }); 
 <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> 

 var link = $("#createAccount"); $(".create p").html("some text&nbsp;").append(link); 
 <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> 

I will answer my own question.我会回答我自己的问题。 There are ways to achieve this with JavaScript, but that would not be best practice and there is an easier way that does not involve JavaScript.有一些方法可以通过 JavaScript 实现这一点,但这不是最佳实践,还有一种更简单的方法,不涉及 JavaScript。

Given that this is an implementation of Azure B2C using a custom policy, this text is best handled in the policy using localization.鉴于这是使用自定义策略的 Azure B2C 的实现,最好在使用本地化的策略中处理此文本。

For example:例如:

 <LocalizedResources Id="api.signuporsignin.en">
    <LocalizedStrings>
      <LocalizedString ElementType="UxElement" StringId="createaccount_one_link">Sign up now</LocalizedString>
      <LocalizedString ElementType="UxElement" StringId="createaccount_intro">Don't have an account?</LocalizedString>
    </LocalizedStrings>
  </LocalizedResources>

Try using document.write and update what you need 尝试使用document.write并更新所需的内容

    document.write("<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>");

Do you have to use jQuery to do this? 您必须使用jQuery来做到这一点吗? If not and you can just use plain old javascript, why not try document.getElementById('createAccount').innerHTML = 'some text'; 如果没有,您可以只使用普通的旧javascript,为什么不尝试 document.getElementById('createAccount').innerHTML = 'some text';

So after understanding what you were asking, I was able to accomplish this with the below: 因此,在了解了您的要求之后,我可以通过以下方法完成此任务:

<p id="MP"> Some text to replace <a href="#">My link</a> </p> 
<br><br> <input type="button" value="Change" onclick="Change()"/> 

<script type="text/javascript">

    function Change() { var Markup = document.getElementById('MP').innerHTML; var POS = Markup.indexOf('<a'); Markup = Markup.substring(POS); Markup = 'Some new text ' + Markup; document.getElementById('MP').innerHTML = Markup; }

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不更改元素的子元素的情况下更改元素的文本? - How can I change an element's text without changing its child elements? 如何在不更改其当前元素的情况下更改其文本? - How can I change an element's text without changing its current element? 如何在不影响元素的 rest 和任何事件侦听器的情况下覆盖元素文本的一部分 - How can I overwrite a portion of an element's text without affecting the rest of the element and any event listeners 如何阻止JavaScript函数影响子元素? - How can I stop a JavaScript function from affecting a child element? 如何动态缩放 HTML 元素而不影响其父元素? - How to dynamically zoom a HTML element without affecting its parents? 如何在不影响父元素的情况下设置子元素的 contenteditable 属性? - How to set child's contenteditable property without affecting the parent element? 如何只使一个子元素粘性而不影响其余部分? - How to make only one child element sticky without affecting the rest? 更改元素的文本而不更改子元素 - change text of an element without changing child elements 如何在不影响子元素的情况下更改div中的文本 - How to change text in div without affecting child elements 更改文本时如何更新 Note 元素? - How can I get my Note element to update when I change its text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM