就多个函数运行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");
});
答案 0 :(得分: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();
});
答案 1 :(得分: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>