jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 删除&害羞;元素中的(软连字符)实体大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试删除所有&害羞;我的元素中的实体(软连字符),

我一直试图用jquery来做这件事.
当我从包含实体的html元素中获取文本时,我似乎得到一个字符串,其中实体被“隐藏”或无法编辑.

你是否必须做一些特别的事情来实际获得包含实体的字符串?

$( document ).ready(function(){
  $("button").on("click",function(){
    var html = $("div > span").html();
    var newHtml = html.replace("­","");
    
    $("div > span").html(newHtml);
  });
});
div{
  max-width: 50px;
  padding: 10px;
  border: 1px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span>My text will be hyphe&shy;ned</span>
</div>
<button>
  Remove Hyphens
</button>

解决方法

使用正则表达式:

var newHtml = html.replace(/\&shy;/gi,"");

注意:您可能还需要检查&#173;,因为浏览器会将其视为数字,而不是人类友好的字符.

说明:

/(\&shy;)/gi
  1st Capturing group (\&shy;)
    \& matches the character & literally
    shy; matches the characters shy; literally (case insensitivE)
    g modifier: global. All matches (don't return on first match)
    i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

片段

$( document ).ready(function(){
  $("button").on("click",function(){
    var html = $("div > span").html();
    var newHtml = html.replace(/(\&shy;|­|&#173;)/gi,"");
    
    $("div > span").html(newHtml);
  });
});
div{
  max-width: 50px;
  padding: 10px;
  border: 1px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span>My text will be hyphe&shy;ned</span>
</div>
<button>
  Remove Hyphens
</button>

Regex101:https://regex101.com/r/wD3oX7/1

大佬总结

以上是大佬教程为你收集整理的jquery – 删除&害羞;元素中的(软连字符)实体全部内容,希望文章能够帮你解决jquery – 删除&害羞;元素中的(软连字符)实体所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。