1

使用以下 jQuery:

jQuery.fn.extend({
    resizelis: function(xWidth) {
      var mButtons = this.length;
      var buttonSize = (xWidth / mButtons);
        return this.each(function() {
            this.css("width", buttonSize + "px");
            this.children("a").css("width", buttonSize + "px");
        });
    }
});

我使用这样的方法:

var $width = $window.scrolltop() > $("#header_image").outerHeight() ? .92 * $(window).width() : .92 * $(".content").width();
$(".menu").children("li").resizelis($width);

HTML

<ul class="menu">
    <li class="current"><a accesskey="1" href="index.html">Home</a></li>
    <li><a accesskey="2" href="highlights.html">Highlights</a></li>
    <li><a accesskey="3" href="agenda.html">Agenda</a></li>
    <li><a accesskey="4" href="tracks.html">Tracks</a></li>
    <li><a accesskey="5" href="sponsors.html">Sponsors / Exhibits</a></li>
    <li><a accesskey="6" href="travel.html">Travel</a></li>
    <li><a accesskey="7" href="register.html">Register</a></li>
</ul>

但这给了我一个错误。我到底在做什么错?谢谢。

4

1 回答 1

2

在调试您的代码时,我发现在您使用的扩展方法中,this而不是$(this),并且您尝试将 css 方法与 一起使用this,这不是 jQuery 对象。尝试更改this$(this),然后重试:

jQuery.fn.extend({
    resizelis: function(xWidth) {
      var mButtons = this.length; //same as with the jquery object $(this)
      var buttonSize = (xWidth / mButtons);
        return $(this).each(function() 
       {
            $(this).css("width", buttonSize + "px");
            $(this).children("a").css("width", buttonSize + "px");
        });
    }
});

each循环内部,this是一个HTMLLIElement,要使用 css 方法,请使用$(this).

于 2013-09-10T16:15:41.603 回答