【问题标题】:Find html character throughout all of website and reduce size在整个网站中查找 html 字符并减小大小
【发布时间】:2017-06-15 06:26:04
【问题描述】:

查找html字符并减小大小,不知道这个功能是什么?

 jQuery("body").children().each(function () {
    jQuery(this).html( jQuery(this).html().match("•").attr('style', "font-size:'9px'"));
 });

【问题讨论】:

  • 您不能为文本指定样式。它必须分配给一个元素
  • css 必须是元素。添加 div。
  • @roshambo 打开你的控制台,它在大喊错误。 jQuery(...).html(...).match(...).attr is not a function.
  • 确定这是实现这个的一些功能吗?我在整个网站上有数百个这样的字符我想保持文本的字体大小但减小这个字符的字体大小。谢谢

标签: javascript html string character


【解决方案1】:

如评论,

您必须将样式设置为元素而不是字符串。

为此,您必须获取字符串,然后将匹配的字符串包装在具有必要样式的元素中。


在您的代码jQuery(this).html().match("•").attr('style', "font-size:'9px'") 中,.match 将返回一个匹配值数组。它们仍然是字符串而不是 HTML 元素。


示例

document.getElementById("btn").addEventListener("click", handleClick);

function handleClick(){
  var input = document.getElementById("input").value;
  if(!input) return;
  
  var p = document.querySelector("p");
  var parsed = p.innerHTML.replace(new RegExp(input, "gi"), function(t){
    return "<span class='highlight'>" + t + "</span>"
  });
  p.innerHTML = parsed;
}
.highlight{
  font-size: 12px;
  border-bottom: 1px dashed gray;
}

span{
  font-size: 16px;
}
<p> This is a sample Text</p>
<input type="text" id="input" />
<button id="btn">Update Style</button>

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 1970-01-01
    • 2020-01-27
    • 2012-01-10
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多