简体   繁体   English

更改jquery选择范围

[英]Change jquery selection scope

When I am writing a jQuery selectors within a specific-page javascript, I'd like to be able to use a simplified (scoped) selection mechanism. 当我在特定页面的javascript中编写jQuery选择器时,我希望能够使用简化(范围)选择机制。

To reference the element currently I need to use the full selector: 要引用当前元素,我需要使用完整选择器:

$('#home-view #events-cloud')'

Since the code line above is used within a home-view backing js file, I'd like to be able to do something like this: 由于上面的代码行在home-view支持js文件中使用,我希望能够做到这样的事情:

$.SetSelectorPrefix('#home-view');
$('#events-cloud');

Is there a possibility to do something to address the code-location specific selections? 是否有可能做一些事情来解决特定于代码位置的选择?

Simply modify the default jQuery init function so that the context is the one chosen by you: 只需修改默认的jQuery init函数,以便上下文是您选择的上下文:

jQuery.noConflict();
$ = function (selector, context) {
    return new jQuery.fn.init(selector, context || document.getElementById('home-view'));
};
$.fn = $.prototype = jQuery.fn;
jQuery.extend($, jQuery);
console.log($('#events-cloud'));

Explanation: 说明:

jQuery.noConflict();

This line prevent jQuery's default assign to $ . 此行阻止jQuery的默认分配给$

$ = function (selector, context) {
    return new jQuery.fn.init(selector, context || document.getElementById('wrapper'));
};

Assign to $ the new modified function that will return a new jQuery instance with the context modified for your purposes(this is the clue of the script)! 分配给$新修改的函数,该函数将返回一个新的jQuery实例,其中包含为您的目的修改的上下文(这是脚本的线索)!

$.fn = $.prototype = jQuery.fn;

Creates a new property fn to the $ function, and assign it to the prototype inheriting properties from jQuery.fn . $ function创建一个新属性fn ,并将其分配给jQuery.fn的原型继承属性。

jQuery.extend($, jQuery);

Extends the created object with jQuery functions, so you can use it exactly as jQuery. 使用jQuery函数扩展创建的对象,因此您可以将其完全用作jQuery。

See a working fiddle of this snippet. 看看这个片段的工作小提琴

You could use something like this: 你可以使用这样的东西:

var container = $('#home-view');
// Will only search elements within #home-view
container.find('#events-cloud');
// Changing container
container = $('#main-view');
// Will only search elements within #main-view
container.find('anotherSelector');

I suppose you need genericity on your generic sections, otherwise you would not ask. 我想你的通用部分需要通用性,否则你不会问。

So a generic code could call $('#selector') without being aware of the context. 因此,通用代码可以在不知道上下文的情况下调用$('#selector')

The simplest way: use a custom function $$ 最简单的方法:使用自定义函数$$

function $$(selector, context) {
    return $(selector, context ? $(context, $$.stack[0]) : $$.stack[0]);
}
$$.stack = [];
$$.push = function(context){ $$.stack.unshift(context); }
$$.pop = function(){ return $$.stack.shift(); }

In your generic sections use $$('#selector') instead of $('#selector') 在通用部分中使用$$('#selector')而不是$('#selector')

Around your generic code execution use: 围绕您的通用代码执行使用:

$$.push('#home-view');

// generic code call using $$
// e.g. $$('#events-cloud') <=> $('#events-cloud', '#home-view') <=> $('#home-view #events-cloud')

$$.pop();

With this custom function, you can nest contexts and even use the $$(selector, localcontext) form. 使用此自定义函数,您可以嵌套上下文甚至使用$$(selector, localcontext)表单。 Thus for the generic code, the current global context is totally transparent. 因此,对于通用代码,当前的全局上下文是完全透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM