0

就在多个函数上运行 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");

});
4

2 回答 2

0

我认为您没有正确使用 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();

});
于 2012-08-31T19:00:04.507 回答
0

弄清楚了。在做了一些试验和错误之后,我能够通过将变量翻转$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>
于 2012-09-03T04:57:12.240 回答