菜鸟教程小白 发表于 2022-12-13 05:38:17

jQuery 和 iOS 设备


                                            <p><p>我有 html 格式的纯文本。我将类添加到某些标签以使用 jQuery 即时格式化它们。</p>

<p>在 PC 和 Android 上一切正常,但在 iOS 上没有任何反应。可能是什么错误?</p>

<p>示例代码如下</p>

<pre><code>&lt;script&gt;
var $=jQuery.noConflict();
jQuery( document ).ready(function() {
    var dialog = $( &#34;p:has(strong)&#34; ).addClass( &#34;wi-dialog&#34; );
    var chapter = $( &#34;p:has(ins)&#34; ).addClass( &#34;wi-chapter&#34; );
    dialog.html(function(i, v) { return v.replace(/(?&lt;=\n)\s*(\(.*?\)).*?/g, &#39;&lt;span class=&#34;wi-remark&#34;&gt;$&amp;&lt;/span&gt;&#39;) });

    var more = $(&#34;.wi-remark:contains(MORE)&#34;).addClass( &#34;wi-more&#34;).removeClass( &#34;wi-remark&#34;);

    var p = $(&#39;p:not(.wi-dialog,.wi-chapter)&#39;);
    p.filter(function() {
      return ((/^+$/).test($(this).html()));
    }).addClass(&#34;wi-number&#34;);
    p.filter(function() {
      return ((/^(THE END)+$/).test($(this).html())); }).addClass( &#34;wi-end&#34; );
    });
});
&lt;/script&gt;
</code></pre>

<p>问题来了</p>

<pre><code>dialog.html(function(i, v) { return v.replace(/(?&lt;=\n)\s*(\(.*?\)).*?/g, &#39;&lt;span class=&#34;wi-remark&#34;&gt;$&amp;&lt;/span&gt;&#39;) });
</code></pre>

<p>文本示例</p>

<pre><code>&lt;p&gt;&lt;strong&gt;HENRY&lt;/strong&gt;
Gather more wood.&lt;/p&gt;

&lt;p&gt;Fitzgerald waits for Henry to turn, then gives his back an exaggerated salute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FITZGERALD&lt;/strong&gt;&lt;br&gt;
(under his breath)&lt;br&gt;
Shame my Pap was a broken down drunk. Else he could’ve bought me a Captain’s job too.&lt;/p&gt;

&lt;p&gt;Boone snickers. Fitzgerald stomps his boot onto a branch, easily snaps it into two easy-to-carry pieces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FITZGERALD&lt;/strong&gt; (CONT’D)&lt;br&gt;
We got a plan for these fires, Captain, or are we roastin’ berries all the way up to Fort Union?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HENRY&lt;/strong&gt;&lt;br&gt;
Glass and the others will be back with some game, Fitzgerald. Just make sure you have the fires ready.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FITZGERALD&lt;/strong&gt;&lt;br&gt;
My supper’s in the hands of a injun- lover, a peach-fuzz kid and a half- wit dummy. Hell, my belly feels full already.&lt;/p&gt;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>此解决方案应该适合您。问题是负面的后视,这在某些 iOS 版本上不起作用。现在它正在使用非捕获组。</p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function(){
var dialog = $( &#34;p:has(strong)&#34; ).addClass( &#34;wi-dialog&#34; )
for (var i = 0; i &lt; dialog.length; i++) {
    e = dialog.eq(i)
    var e_html = e.html()
    var pattern = /(?:\n)\s*(\(.*?\)).*?/gm
    var match = pattern.exec(e_html)
    while (match != null) {
      e_html = e_html.replace(match, &#39;&lt;span class=&#34;wi-remark&#34;&gt;$&amp;&lt;/span&gt;&#39;)
      match = pattern.exec(e_html)
    }
    e.html(e_html)
}
})</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&#34;UTF-8&#34;&gt;
    &lt;script src=&#34;https://code.jquery.com/jquery-3.1.0.min.js&#34;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;p&gt;
      &lt;strong&gt;FITZGERALD&lt;/strong&gt; (CONT’D)&lt;br&gt;
      Shut up, boy, you don’t get no say in this.&lt;br&gt;
      (back to Henry)&lt;br&gt;
      And in case you hadn’t noticed, Captain, we’re seventeen men short of what we were.
      (off the wounded trapper)&lt;br&gt;
      Eighteen before long.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;strong&gt;FITZGERALD&lt;/strong&gt;&lt;br&gt;
      (under his breath)&lt;br&gt;
      Shame my Pap was a broken down drunk. Else he could’ve bought me a Captain’s job too.
    &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

                                                <p style="font-size: 20px;">关于jQuery 和 iOS 设备,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/57728760/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/57728760/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: jQuery 和 iOS 设备