相当于jquery nearest()的javascript是什么? [英] What is the javascript equivalent to jquery closest()?

查看:61
本文介绍了相当于jquery nearest()的javascript是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它是用DOM API而不是jquery编写的。我不知道为什么。它位于jquery表单验证的提交函数中。我需要更改parentNode部分,以便它不是父,而是最接近。我对javascript不好。我试着把它转换成jquery,但是无法让它工作。基本上,如果这是jquery,我需要将.parent()更改为.closest()。

I have the following code, which is written in DOM API instead of jquery. I'm not sure why. It's inside the submit function for jquery form validation. I need to change the "parentNode" part so that instead of "parent" it is "closest." I'm no good with javascript. I tried my hand at converting this to jquery, but wasn't able to get that to work. Basically, if this were jquery, I'd need to change .parent() to .closest().

var summary = "";

$.each(element, function() { 
summary += "<li>" + this.element.parentNode.getElementsByTagName('label')[0].innerHTML.replace("\<span\>*\<\/span\>","") + "</li>"; 
});

summary = summary.replace(/\<li\>.+\<\/li\>\1/, "$1");

alert(summary);

这可能与javascript有关吗?或者,是否有一种简单的方法将其转换为jquery?

Is this possible to do with javascript? Or, is there an easy way to convert this to jquery?

更新:这是一个帮助解释我正在尝试的内容的小提琴完成。基本上,因为我在其中一个输入周围添加了span标记,所以'p'标记不再是父标记。因此,parentNode不再找到'p'标签。

UPDATE: Here is a fiddle to help explain what I'm trying to accomplish. Basically, because I added a 'span' tag around one of the inputs, the 'p' tag is no longer the parent. As such, the parentNode is not finding the 'p' tag anymore.

http://jsfiddle.net/9um8xt79/1/

http://jsfiddle.net/9um8xt79/1/

更新2 - 之前的小提琴链接不对。上面的链接是正确的。

UPDATE 2 -- THE previous fiddle link was incorrect. The link above is the right one.

我如何修改以查找< p> 标记,无论是直接父母还是祖父母?

How can I modify to find the <p> tag regardless of whether it is the direct parent, or a grandparent?

推荐答案

这是.closest属性的javascript:

Here's the javascript for the .closest property:

closest: function( selectors, context ) {
        var cur,
            i = 0,
            l = this.length,
            matched = [],
            pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
                jQuery( selectors, context || this.context ) :
                0;

        for ( ; i < l; i++ ) {
            for ( cur = this[i]; cur && cur !== context; cur = cur.parentNode ) {
                // Always skip document fragments
                if ( cur.nodeType < 11 && (pos ?
                    pos.index(cur) > -1 :

                    // Don't pass non-elements to Sizzle
                    cur.nodeType === 1 &&
                        jQuery.find.matchesSelector(cur, selectors)) ) {

                    matched.push( cur );
                    break;
                }
            }
        }

每当你想要弄清楚是什么在jQuery中完成,你可以随时查看jQuery源代码并将其反向工程到你的场景中。虽然有时会无法解决问题,但是我的问题就是这样。这是我开始的地方。

Whenever you're trying to figure out something is done in jQuery, you can always just take a look at the jQuery source and reverse engineer it to your scenario. Although there will be times when this won't work, the way your question is phrased, this is where I'd start.

来源: http://code.jquery.com/jquery-2.1.1.js

这篇关于相当于jquery nearest()的javascript是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆