最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I use the jQuery-like selector in pure JavaScript - Stack Overflow

programmeradmin2浏览0评论

What I looking for is:

var arrinput = $('input[name$="letter"]')

How can I change that from jQuery style to a pure javascript style?

So I want the <input> tags which name is ended with "letter".


I changed code a bit... My browser dont support querySelector and FYI I'm using webbrowser component on c# winforms

What I looking for is:

var arrinput = $('input[name$="letter"]')

How can I change that from jQuery style to a pure javascript style?

So I want the <input> tags which name is ended with "letter".


I changed code a bit... My browser dont support querySelector and FYI I'm using webbrowser component on c# winforms

Share Improve this question edited Sep 14, 2011 at 8:33 Mr.Rendezvous asked Sep 14, 2011 at 3:02 Mr.RendezvousMr.Rendezvous 1,9935 gold badges20 silver badges34 bronze badges 3
  • 2 coz I dont want to include jQuery.js to my page :) – Mr.Rendezvous Commented Sep 14, 2011 at 3:14
  • 1 coz I'm in a situation which I prefer not to have a jQuery library. – Mr.Rendezvous Commented Sep 14, 2011 at 3:21
  • 3 Your answer, as well as the answer to "why should I use jQuery instead of doing this myself" is to merge Hemlock's querySelector answer with BenjaminHarris's brute-force search so you can fall back if querySelector isn't available in your browser. Or, use jQuery which does all this for you. – user229044 Commented Sep 14, 2011 at 3:22
Add a comment  | 

3 Answers 3

Reset to default 15

For modern browsers:

document.querySelector('input[name$=letter]');

will return the first match.

document.querySelectorAll('input[name$=letter]');

will return a list of matches.

I suspect that if you look through the jquery source code, it uses document.querySelector[All] when it's available.

Try:

var items = document.getElementsByTagName('input');
for (var i=0; i<items.length; i++) {
    var item = items[i];
    if (/letter$/.test(item.name)) {
        item.value = "A letter";
    }
}

It's an old post, but I searched a similar solution and I haven't found it.

So I've doing a little function to do that (it's optimizable):

/**
 * Searches and finds the parent node for a dom object
 * 
 * @examples: 
 *  getParent(elem, 'div')                  // The first div parent found
 *  getParent(elem, 'div[id]')              // The first div parent with an id found
 *  getParent(elem, 'div[id="toto"]')       // The first div parent with id equals toto found
 *  getParent(elem, 'div[id=^="toto"]')     // The first div parent with id start by toto found
 *  getParent(elem, 'div[id=$="toto"]')      // The first div parent with id end by toto found
 *  getParent(elem, 'div[id=*="toto"]')     // The first div parent with id contains toto found
 *
 * @param domObject elem
 * @param string [target]
 * @return domObject or null
 */
function getParent(elem, target)
{
    if(target == null)
        return elem.parentNode;

    var elem_name = target,
        attr_name = null, attr_value = null, 
        compare_type = null,
        match_val = target.match(/\[.+[^\[\]]\]$/i);

    if(match_val != null) 
    {
        elem_name = elem_name.replace(match_val[0], '');

        var expr = match_val[0].substr(1, match_val[0].length-2),
            tmp = expr.split('=');

        attr_name = tmp[0];
        if(tmp.length == 2) 
        {
            attr_value = tmp[1].toLowerCase();
            attr_value = attr_value.replace(/(\'|\")+/ig, '');

            if(attr_name.match(/\^$/))
                compare_type = 'begin';
            else if(attr_name.match(/\*$/))
                compare_type = 'all';
            else if(attr_name.match(/\$$/))
                compare_type = 'end';
            else
                compare_type = 'simple';

            if(compare_type != 'simple')
                attr_name = attr_name.substr(0, attr_name.length-1);
        }
    }

    var parent = elem.parentNode;

    do 
    {
        if(parent.nodeName.toUpperCase() == elem_name.toUpperCase())
        {
            if(attr_name != null) 
            {
                var attribute = parent.getAttribute(attr_name).toLowerCase();
                if(attribute != null && attribute != '') 
                {
                    if(attr_value == null)
                        return parent;

                    if(compare_type == 'simple' && attribute == attr_value)
                        return parent;
                    if(compare_type == 'begin' && attribute.match(eval('/^'+attr_value+'/ig')))
                        return parent;
                    if(compare_type == 'end' && attribute.match(eval('/'+attr_value+'$/ig')))
                        return parent;
                    if(compare_type == 'all' && attribute.match(eval('/'+attr_value+'/ig')))
                        return parent;
                }
            } else {
                return parent;
            }
        }

        parent = parent.parentNode;
    } 
    while(parent != null);

    return null;
}
发布评论

评论列表(0)

  1. 暂无评论