【问题标题】:Excluding Links from Javascript从 Javascript 中排除链接
【发布时间】:2017-07-20 15:54:30
【问题描述】:

我想首先说我对 javascript 非常陌生,基本上做了很多谷歌搜索来找到这个地方和各种其他资源。虽然我找到了一个脚本来修改我想要的东西(并设法让它工作),但它会干扰在指定 div 中创建的任何链接。有没有办法从 javascript 中排除链接,让 javascript 影响文本?

这是javascript。虽然让第一部分工作(我替换引用的文本)没有问题,但我似乎无法排除包含 html 且其中包含引号的链接和图像。

$("div.gam-posttem div").each(function(){
    this.innerHTML = this.innerHTML.replace(/\"([^\"]+)\"/g, 
        '<div class="gam-quotetext">"$1"</div>');
});
$not('div.gam-posttem > div > a').each(function(){});

这是我正在使用的 html。

<div class="gam-posttem"><div class="gam-posttem1">

"quote here" and regular text here. "more quote here"

<br><br><a href="http://www.google.com">Link is Here</a>

</div></div>

非常感谢任何帮助,如果您需要更多信息,例如 CSS,请随时询问。

【问题讨论】:

  • 您究竟希望 javascript 对您的链接做什么?
  • 可能能够更新正则表达式以避免标签内的文本,否则使用内部循环跳过标签

标签: javascript jquery html


【解决方案1】:

让这件事特别棘手的是 JavaScript 正则表达式不允许后视。您想要做的是尝试匹配 " 对,其中有相同数量的 字符(在其他 " 字符之外)。即使你可以,那也是一个看起来很讨厌的正则表达式......

但是,您只关心 对之外的字符,使用正则表达式进行匹配(虽然不建议)是可能的: &lt;(?:[^&gt;&lt;\"\']*?(?:([\"\']).*?\1)?[^&gt;&lt;\'\"]*?)+(?:&gt;|$) 将匹配所有 对,忽略标记内引号内的右尖括号。因此,您希望匹配这些标签之外的所有内容。

可能有更好的方法来做到这一点,但您可以尝试以下想法:

  1. 匹配所有标签
  2. 对于每个标签,获取开始位置和长度 => 计算结束位置
  3. 将字符串的起始索引添加到结束位置的前面
  4. 将字符串的长度添加到起始位置
  5. 对于每个 (end, start) 对(因为我们正在反转匹配项),运行您的替换方法并修改字符串。

String.prototype.spliceish = function(start, end, newSubStr) {
  return this.slice(0, start) + newSubStr + this.slice(end);
};

var tagMatch = /<(?:[^><\"\']*?(?:([\"\']).*?\1)?[^><\'\"]*?)+(?:>|$)/g;
var tokenMatch = /\"([^\"]+)\"/g;

function invertedMatch(htmlString) {
	var returnString = htmlString;
	var startIndexes = [],
  	lengths = [],
    match = tagMatch.exec(htmlString);
  while(match !== null)
  {
  	startIndexes.push(match.index);
    lengths.push(match[0].length);
    match = tagMatch.exec(htmlString);
  }
  
  var endIndexes = startIndexes.map(function(val, ix) { return val + lengths[ix]; });
  var invertedStarts = [0].concat(endIndexes); // we are inverting, so capture start text.
  var invertedEnds = startIndexes.concat(htmlString.length);
  
  // will need to go backwards
  for(var i = invertedStarts.length - 1; i >= 0; i--) {
  	var start = invertedStarts[i],
    	end = invertedEnds[i];
  	var stringReplace = htmlString.substring(start, end);
    returnString = returnString.spliceish(start, end, stringReplace.replace(tokenMatch,
    	'<div class="gam-quotetext">"$1"</div>'));
  };
  
  return returnString;
}

$("#root").html(invertedMatch($("#root").html()));
.gam-quotetext{
  color: green
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="root">
  <div class="gam-posttem">
    <div class="gam-posttem1">

    "quote here" and regular text here. "more quote here"

    <br>
    <br>
    <a href="http://www.google.com">Link is Here</a>

    </div>
  </div>
</div>

【讨论】:

  • 谢谢!然而,虽然我可以让它在 JSFiddle 等中完美地工作,但我似乎无法让它在我的网站上工作,这是一个 Jcink 论坛。我是否将 javascript 包装在简单的
  • 不幸的是,我对 Jcink 一无所知,但是如果您的页面数据来自其他地方,则上述代码可能运行得太早了。尝试将带有 javascript 的 Script 标签放在正文的底部,并可能将 jQuery 部分包装在 $(document).ready()
猜你喜欢
  • 2015-09-25
  • 2011-10-26
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2015-01-26
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多