【问题标题】:jquery noconflict for email spam protectionjquery noconflict 用于电子邮件垃圾邮件保护
【发布时间】:2012-08-31 18:46:41
【问题描述】:

就在多个函数上运行 jquery noconflict 设置而言,我遇到了一些冲突,因此我决定通过针对我正在测试的函数之一测试 noconflict 来隔离问题。

我在放置 s$ 的位置上尝试了多种变体,但似乎没有一种配置有效。我可以完成这项工作的唯一方法是将变量保留为 -> var $,并将所有因变量保留为此设置,但是我需要了解如何使用唯一变量来使其工作?

也许我的语法也有问题?

var s$ = jQuery.noConflict();

s$.fn.emailSpamProtection = function(className) {

return s$(this).find("." + className).each(function() {
var $email = s$(this);
var address = $email.text()
.replace(/\s*\[at\]\s*/, '@')
.replace(/\s*\[dot\]\s*/g, '.');
$email.html('<a href="mailto:' + address + '">'+ address +'</a>');
    });
};

这是我尝试过的修改后的脚本。

jQuery.noConflict();

(function($){
$.fn.emailSpamProtection = function(className) {
 return this.find("." + className).each(function() {
        var $email = this;
        var address = $email.text()
        .replace(/\s*\[at\]\s*/, '@')
        .replace(/\s*\[dot\]\s*/g, '.');
        $email.html('<a href="mailto:' + address + '">'+ address +'</a>');
    });
};
})(jQuery);

我把它放到了我的 .html 主页中

jQuery(function($){

    //Note, you can use $(...) because you are wrapping everything within a jQuery function
    $("body").emailSpamProtection("email");

});

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我认为您没有正确使用 noConflict 属性。这就是我将如何使用它:

    //Establish jQuery noConflict mode.
    jQuery.noConflict();
    
    //Define your jQuery plugins/functions
    (function($){
    $.fn.emailSpamProtection = function(className) {
     return this.find("." + className).each(function() {
            var $email = this;
            var address = $email.text()
            .replace(/\s*\[at\]\s*/, '@')
            .replace(/\s*\[dot\]\s*/g, '.');
            $email.html('<a href="mailto:' + address + '">'+ address +'</a>');
        });
    };
    })(jQuery);
    
    
    // Use jQuery with $(...)
    jQuery(function($){
    
        //Note, you can use $(...) because you are wrapping everything within a jQuery function
        $('#myElement').emailSpamProtection();
    
    });
    

    【讨论】:

    • 好的,我试了一下,但是没有成功显示电子邮件链接。这是我在主页的 jquery 函数中放置的内容。我已经用修改后的脚本更新了我原来的问题。
    【解决方案2】:

    想通了。经过反复试验,我能够通过将变量翻转为 $j 而不是 j$ 来解决问题。这是我的最终结果。

    //JQuery Section
    
    var $j=jQuery.noConflict();
    
      //Hiding other scripts that were included in this application.js file//
    
    
    //email spam protection - Example Markup: <span class="email">name[at]domain[dot]com</span>
    $j.fn.emailSpamProtection = function(className) {
    
    return $j(this).find("." + className).each(function() {
    var email = $j(this);
    var address = email.text()
    .replace(/\s*\[at\]\s*/, '@')
    .replace(/\s*\[dot\]\s*/g, '.');
    email.html('<a href="mailto:' + address + '">'+ address +'</a>');
        });
    };
    
    });
    
    //Script added to the presentation page (html,php,whatever)
    
    <script>
    $j(function() {
    
      $j("body").emailSpamProtection("email"); 
    
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多