douniewei6346 2015-12-17 16:36
浏览 5

使用JQuery,php或其他完整网站搜索和替换

jQuery(document).ready(function () {
    var tel1 = "<p>";
    var tel2 = "<h1>";
    var tel3 = "<h2>";
    var tel4 = "<h3>";

    if(tel4.html = "855-322-8671"){
        tel4.html ="<a href=\"tel:855-322-8671\">1-855-Factor-1</a>";
    }
}

I am trying to figure out how to do a search and replace through the whole site for these numbers and convert it into a <a> so you can call the number. Any ideas with JQuery, or php?

  • 写回答

1条回答 默认 最新

  • douzui6173 2015-12-17 17:37
    关注

    If you are trying to extract the value from all the <p>, <h1>, <h2> and <h3> elements then your code should look like this. I am using the <h3> as an example.

        jQuery(document).ready(function(){
          var tel4 = $("h3"); 
    })
    

    Here we are using jQuery to target all the <h3> elements and store the reference to those <h3> elements in the variable var tel4. It is a good practice to name such variables with a $ before the name, like so $tel4.

    We can then use the jQuery .each() function to loop over all the <h3> elements that were stored in our tel4 variable and see if any of them contain the number 855-322-8671.

    $.each(tel4, function() {
    
        var number = "855-322-8671";
        var replacement = "1-855-Factor-1"
        if ($(this).is(':contains("' + number + '")')) {
            $(this).html(function() {
                return $(this).html().replace(number, replacement);
            })
        }
    })
    

    The above code will search every <h3> that contains the number and replace that number with our replacement.

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度