Jquery wrap() - 正斜杠前的字符`/`

在正斜杠之前,/我可以wrap()在 HTML 标签中输入字符吗?我想将所有字符包装到一个 HTML 元素之前/


jQuery 片段:


var x = jQuery('.elementor-drop-cap-yes p').contents().filter(function(){

  return this.nodeType === 3

}).wrap('<span class="drop-caps" />');

当前结果:


<p>

  <span class="a">

    <span class="b">1</span>

  </span>

  0/10

</p>

预期结果:


<p>

  <span class="a">

    <span class="b">1</span>

  </span>

  <div class="c">

    <b class="d">0</b>/10

  <div>

</p>

提前致谢!


偶然的你
浏览 165回答 1
1回答

米琪卡哇伊

您可以尝试以下简单的逻辑来实现 html 包装jQuery(function(){&nbsp; jQuery('.elementor-drop-cap-yes p').each(function(){&nbsp; &nbsp; &nbsp;var $div = jQuery('<div/>');&nbsp; &nbsp; &nbsp;var $children = $(this).children();&nbsp; &nbsp; &nbsp;$div.append($children);&nbsp; &nbsp; &nbsp;var text = jQuery(this).text();&nbsp; &nbsp; &nbsp;if(text.indexOf('/')>=0) {&nbsp; &nbsp; &nbsp; &nbsp; var index = text.indexOf('/');&nbsp; &nbsp; &nbsp; &nbsp; //console.log(index);&nbsp; &nbsp; &nbsp; &nbsp; var textParts = text.substring(0, index);&nbsp; &nbsp; &nbsp; &nbsp; var newHTML = '<div class="c"><b class="d">' + text.substring(0, index) +'</b>' + text.substring(index, text.length) + '</div>';&nbsp; &nbsp; &nbsp; &nbsp; jQuery(this).empty().append($children).append(newHTML);&nbsp; &nbsp; &nbsp;}&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="elementor-drop-cap-yes"><p>&nbsp; &nbsp; <span class="a">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="b">1</span>&nbsp; &nbsp; &nbsp;</span>&nbsp; &nbsp; 0/10</p></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript