简体   繁体   中英

.find doesn't work in jquery 1.4.2 and I can't figure out how to support it

I can't figure out how to write my code backwards to support jQuery 1.4.2. So I am perplexed trying to support older library files.

    var n = this;
    e(document).ready(function () {
        var r = e("body").find(n);
        r.attr("placeholder", t);
        r.val(t);
        n.focus(function () {
            e(this).val("")
        }).blur(function () {
            var r = e(n);
            if (r.val() == "") r.val(t)
        })
    })

Yes, that would be correct. The .find() function was added in 1.6. You could upgrade jQuery or use the .find() using the CSS selector.

If you have the elements with unique .id elements, you could just change your .find function to call that:

.find('#' + n.id)

Another option, since you have 'this', is that you could just use the basic $(this):

var r = $(this);

Or, for your code r = e(this);

This might not be the most elegant solution, but you could check if the find function exists, and then add it to the jquery object if it does not.

Something like

if (!$.find) {
    $.fn.find = function () {
        // find function definition
    };
}

edit: a better conditional:

var version = $.fn.jquery.split('.');
if (version[0] < 2 && version[1] <= 6 && version[2] < 4)

last edit: since jsfiddle doesn't have less than 1.6.4 jquery, i can't test this to verify, but I pulled the find function from google's cdn jquery 1.6.4, so the complete monkeypatch would be:

var version = $.fn.jquery.split('.');
if (version[0] < 2 && version[1] <= 6 && version[2] < 4) {
$.find = function( expr, context, isXML ) {
var set;

if ( !expr ) {
    return [];
}

for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
    var match,
        type = Expr.order[i];

    if ( (match = Expr.leftMatch[ type ].exec( expr )) ) {
        var left = match[1];
        match.splice( 1, 1 );

        if ( left.substr( left.length - 1 ) !== "\\" ) {
            match[1] = (match[1] || "").replace( rBackslash, "" );
            set = Expr.find[ type ]( match, context, isXML );

            if ( set != null ) {
                expr = expr.replace( Expr.match[ type ], "" );
                break;
            }
        }
    }
}

if ( !set ) {
    set = typeof context.getElementsByTagName !== "undefined" ?
        context.getElementsByTagName( "*" ) :
        [];
}

return { set: set, expr: expr };

};

Are you sure of this ? I have just binned out one example using 1.4.2 for find and it's working properly .

HTML:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body id='bd'>
 <ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a">A</li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1">1</li>
          <li class="item-2">2</li>
          <li class="item-3">3</li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii">III</li>
</ul>
</body>
</html>

JS:

$('li.item-ii').find('li').css('background-color', 'red');

Find the bin here : http://jsbin.com/INonAYo/1/edit

You're overcomplicating your code. First, you don't need to use $(document).ready() inside of a plugin, you can let the developer using the plugin take care of that. Next, inside of a plugin, this is a jQuery object containing the selected elements. You should iterate over them then act on each one individually.

(function ($) {
    $.fn.labelfixer = function (t) {
        return this.each(function(){
            var $this = $(this);
            $this.attr("placeholder",t);
            this.value = t;
            $this.focus(function(){
                if (this.value == t) {
                    this.value = "";
                }
            });
            $this.blur(function(){
                if (this.value == "") {
                    this.value = t;
                }
            });
        });
    };
})(jQuery);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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