当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 2 Fork 1

terrason / jquery-nearby
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Which HTML Attribute do I hate the most? That is id. I wrote this to avoid querying element by id.

This is a jQuery plugin to find the element matching a selector that is somewhere nearby :-)

Image a scenario of User Mobile Binding:

<form class="form-horizontal">
    <div class="form-group">
        <label class="col-xs-2 control-label" for="binding-mobile">
            手机号码:
        </label>
        <div class="col-xs-9">
            <input class="form-control" id="binding-mobile" type="tel" placeholder="手机号码"
                   name="mobile"/>
            <input class="form-control" id="binding-smscode" placeholder="验证码"
                   name="smscode"/>
            <button class="btn btn-link" type="button" id="js-smscode">
                <span class="state normal">获取验证码</span>
                <span class="state alter"><span class="code" data-bind="cooldown">0</span>秒后可重新获取</span>
            </button>
        </div>
    </div>
</form>

Use Jquery Nearby to help you:

var $element=$("#js-smscode");//the base point of searching

$element.nearby(1);  //equals to `.siblings(":eq(1)")`
$element.nearby("#binding-mobile");
$element.nearby('input[name="mobile"]');
$element.nearby("mobile");  // equals to `.nearby("[name=mobile]")`

The above all found #binding-mobile element. Especially the .nearby(name) form works gracefully, Simple string parameter that test failed with /^#/ or /[.[>~:]/ is considered to name, EXCEPT:

body|form|select|input|textarea|div|span|h\d|table|tr|td|th|p|header|section|fieldset

Jquery Nearby find out the target through these steps:

  1. If parameter is int then just call .siblings(":eq()") instead.
  2. Else if parameter is start with # then find global document. (Contains same id in one page is your fault :<)
  3. Else find from closest .form-group.
  4. If find nothing, go more far, and find from closest form context.
  5. If find nothing, continue findind from closest .dialog context.
  6. Finally, find from global document. You can specify a searching order by the second parameter.
$element.nearby("mobile", [".form","#myDialog"]);

So at last Jquery Nearby WON'T return undefined|null|false , it returns EMPTY JQUERY object if found nothing.

The MIT License (MIT) Copyright (c) 2017 terrason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

jQuery Nearby Plugin 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/terrason/jquery-nearby.git
git@gitee.com:terrason/jquery-nearby.git
terrason
jquery-nearby
jquery-nearby
master

搜索帮助