jQuery 和 iOS 设备

标签 jquery ios browser

我有 html 格式的纯文本。我向某些标签添加类,以便使用 jQuery 即时格式化它们。

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

示例代码如下

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

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

    var p = $('p:not(.wi-dialog,.wi-chapter)');
    p.filter(function() { 
        return ((/^[0-9.]+$/).test($(this).html())); 
    }).addClass("wi-number");
    p.filter(function() {
        return ((/^(THE END)+$/).test($(this).html())); }).addClass( "wi-end" );
    });
});
</script>

问题就在这里

dialog.html(function(i, v) { return v.replace(/(?<=\n)\s*(\(.*?\)).*?/g, '<span class="wi-remark">$&</span>') });

文本示例

<p><strong>HENRY</strong>
Gather more wood.</p>

<p>Fitzgerald waits for Henry to turn, then gives his back an exaggerated salute.</p>

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

<p>Boone snickers. Fitzgerald stomps his boot onto a branch, easily snaps it into two easy-to-carry pieces.</p>

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

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

<p><strong>FITZGERALD</strong><br>
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.</p>

最佳答案

这个解决方案应该适合您。问题在于负向后查找,这在某些 iOS 版本上不起作用。现在它改用非捕获组。

$(document).ready(function(){
  var dialog = $( "p:has(strong)" ).addClass( "wi-dialog" )
  for (var i = 0; i < 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[1], '<span class="wi-remark">$&</span>')
      match = pattern.exec(e_html)
    }
    e.html(e_html)
  }
})
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
  </head>
  <body>
    <p>
      <strong>FITZGERALD</strong> (CONT’D)<br>
      Shut up, boy, you don’t get no say in this.<br>
      (back to Henry)<br>
      And in case you hadn’t noticed, Captain, we’re seventeen men short of what we were.
      (off the wounded trapper)<br>
      Eighteen before long.
    </p>
    <p>
      <strong>FITZGERALD</strong><br>
      (under his breath)<br>
      Shame my Pap was a broken down drunk. Else he could’ve bought me a Captain’s job too.
    </p>
  </body>
</html>

关于jQuery 和 iOS 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728760/

相关文章:

ios - 调试和发布的区别

html - 创建用于更改主题而不是浏览器页面样式 html 的开关

c# - 新线程中的 WebBrowser 控件

javascript - jQuery div 或使用输入字段名称

javascript - Jquery 中的延迟操作

javascript - 使用 JQuery 将 JSON 数据传递到 DIV 中

java - 处理来自应用程序的非 UI 部分的导航?

javascript - Jquery 工具提示验证文件

ios - Apple 开发者注册过期后,您还可以从 iOS 访问您的应用程序吗?

html - HTML 选择事件