使用脚本更改文本片段

huangapple go评论65阅读模式
英文:

Change piece of text using script

问题

I'm trying to change only the word Phone: in the second last line to Tel. in the following code block. The 123-456-7890 would stay after the change. I've tried this different ways and nothing works. Can someone provide me with a JavaScript or jQuery script to do that? Here is the code block.

After the script runs, the resulting HTML would be:

Thank you for your advice!

I've tried:

But I don't want to replace all of the <p>, just a piece of it.

英文:

I'm trying to change only the word Phone: in the second last line to Tel. in the following code block. The 123-456-7890 would stay after the change. I've tried this different ways and nothing works. Can someone provide me with a JavaScript or jQuery script to do that? Here is the code block.

 &lt;div class=&quot;staffdirectory imagebase section&quot;&gt;
    &lt;div class=&quot;staff_member&quot;&gt;
    &lt;div class=&quot;staff_photo nophoto&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;staff_name nophoto-name&quot;&gt;
    &lt;p&gt;&lt;span class=&quot;staff_name_bolded&quot;&gt;Lname, Fname&lt;/span&gt;&lt;/p&gt;
    &lt;p&gt;&lt;span&gt;IT Department&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
    &lt;div class=&quot;staff_contact nophoto-contact&quot;&gt;
    &lt;p&gt;123 Building Center&lt;/p&gt;
    &lt;p&gt;Phone: 123-456-7890&lt;/p&gt;
    &lt;/div&gt;&lt;div class=&quot;clear_all&quot;&gt;&lt;/div&gt;&lt;/div&gt;

After the script runs, the resulting HTML would be:

   &lt;div class=&quot;staffdirectory imagebase section&quot;&gt;
   &lt;div class=&quot;staff_member&quot;&gt;
   &lt;div class=&quot;staff_photo nophoto&quot;&gt;&lt;/div&gt;
   &lt;div class=&quot;staff_name nophoto-name&quot;&gt;
   &lt;p&gt;&lt;span class=&quot;staff_name_bolded&quot;&gt;Lname, Fname&lt;/span&gt;&lt;/p&gt;
   &lt;p&gt;&lt;span&gt;IT Department&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
   &lt;div class=&quot;staff_contact nophoto-contact&quot;&gt;
   &lt;p&gt;123 Building Center&lt;/p&gt;
   &lt;p&gt;Tel. 123-456-7890&lt;/p&gt;
   &lt;/div&gt;&lt;div class=&quot;clear_all&quot;&gt;&lt;/div&gt;&lt;/div&gt;

Thank you for your advice!

I've tried:

   &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js&quot;&gt;&lt;/script&gt;
   &lt;script&gt;
   $(document).ready(function(){
   $(&#39;#demo&#39;).text(&#39;The replaced text.&#39;);
   });
   &lt;/script&gt;
   &lt;div id=&quot;demo&quot;&gt;
      &lt;p&gt;The initial text&lt;/p&gt;
   &lt;/div&gt;

But I don't want to replace all of the &lt;p&gt;, just a piece of it.

答案1

得分: 1

使用 :contains() 选择器来查找包含 Phone:<p> 元素并进行替换。

$(".staff_contact p:contains(Phone:)").text(function(i, text) {
    return text.replace('Phone:', 'Tel:');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="staffdirectory imagebase section">
  <div class="staff_member">
    <div class="staff_photo nophoto"></div>
    <div class="staff_name nophoto-name">
      <p><span class="staff_name_bolded">Lname, Fname</span></p>
      <p><span>IT Department</span></p>
    </div>
    <div class="staff_contact nophoto-contact">
      <p>123 Building Center</p>
      <p>Phone: 123-456-7890</p>
    </div>
    <div class="clear_all"></div>
  </div>
</div>
英文:

Use the :contains() selector to find the &lt;p&gt; that has Phone: and perform the replacement.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.staff_contact p:contains(Phone:)&quot;).text(function(i, text) {
    return text.replace(&#39;Phone:&#39;, &#39;Tel:&#39;);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;staffdirectory imagebase section&quot;&gt;
  &lt;div class=&quot;staff_member&quot;&gt;
    &lt;div class=&quot;staff_photo nophoto&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;staff_name nophoto-name&quot;&gt;
      &lt;p&gt;&lt;span class=&quot;staff_name_bolded&quot;&gt;Lname, Fname&lt;/span&gt;&lt;/p&gt;
      &lt;p&gt;&lt;span&gt;IT Department&lt;/span&gt;&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;staff_contact nophoto-contact&quot;&gt;
      &lt;p&gt;123 Building Center&lt;/p&gt;
      &lt;p&gt;Phone: 123-456-7890&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;clear_all&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

我在所有包含电话号码的 <p> 元素中添加了 class="PhoneNumber",然后对它们运行了一个简单的替换脚本,如下所示:

collection = document.getElementsByClassName("PhoneNumber")

Array.from(collection).forEach(element => {
    string = element.innerText.replaceAll("Phone:", "Tel:")
    element.innerText = string
})
英文:

I added a class=&quot;PhoneNumber&quot; to all &lt;p&gt; that contain a phone number and then run a simple replace script on them as follows

collection = document.getElementsByClassName(&quot;PhoneNumber&quot;)

Array.from(collection).forEach(element =&gt; {
    string = element.innerText.replaceAll(&quot;Phone:&quot;, &quot;Tel:&quot;)
    element.innerText = string
})

huangapple
  • 本文由 发表于 2023年3月7日 00:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定