【问题标题】:JQuery only allows one button with this codeJQuery 只允许使用此代码的一个按钮
【发布时间】:2011-07-27 08:29:46
【问题描述】:

更新

我试图在多个 div 中包含相同的一组按钮。似乎我只能在一个 div 上包含按钮集。它一次只会出现在一个位置。我希望按钮显示在我设置跨度的任何位置。帮助!这是我的代码:

    (function($){

// Creating the jQuery plugin:
$.fn.sweetPages = function(opts){

    // If no options were passed, create an empty opts object
    if(!opts) opts = {};

    var resultsPerPage = opts.perPage || 3;

    // The plugin works best for unordered lists, althugh ols would do just as well:
    var ul = this;
    var li = ul.find('li');

    li.each(function(){
        // Calculating the height of each li element, and storing it with the data method:
        var el = $(this);
        el.data('height',el.outerHeight(true));
    });

    // Calculating the total number of pages:
    var pagesNumber = Math.ceil(li.length/resultsPerPage);

    // If the pages are less than two, do nothing:
    if(pagesNumber<2) return this;

    // Creating the controls div:
    var swControls = $('<div class="swControls">');

    for(var i=0;i<pagesNumber;i++)
    {
        // Slice a portion of the lis, and wrap it in a swPage div:
        li.slice(i*resultsPerPage,(i+1)*resultsPerPage).wrapAll('<div class="swPage" />');

        // Adding a link to the swControls div:
        swControls.append('<a href="" class="swShowPage">'+(i+1)+'</a>');
    }

    ul.append(swControls);

    var maxHeight = 0;
    var totalWidth = 0;

    var swPage = ul.find('.swPage');
    swPage.each(function(){

        // Looping through all the newly created pages:

        var elem = $(this);

        var tmpHeight = 0;
        elem.find('li').each(function(){tmpHeight+=$(this).data('height');});

        if(tmpHeight>maxHeight)
            maxHeight = tmpHeight;

        totalWidth+=elem.outerWidth();

        elem.css('float','left').width(ul.width());
    });

    swPage.wrapAll('<div class="swSlider" />');

    // Setting the height of the ul to the height of the tallest page:
    ul.height(maxHeight);

    var swSlider = ul.find('.swSlider');
    swSlider.append('<div class="clear" />').width(totalWidth);

    var hyperLinks = ul.find('a.swShowPage');

    hyperLinks.click(function(e){

        // If one of the control links is clicked, slide the swSlider div 
        // (which contains all the pages) and mark it as active:

        $(this).addClass('active').siblings().removeClass('active');

        swSlider.stop().animate({'margin-left':-(parseInt($(this).text())-1)*ul.width()},'slow');
        e.preventDefault();
    });

    // Mark the first link as active the first time this code runs:
    hyperLinks.eq(0).addClass('active');

    // Center the control div:
    swControls.css({
        'left':'50%',
        'margin-left':-swControls.width()/2
    });

    return this;

}})(jQuery);


$(document).ready(function(){
    /* The following code is executed once the DOM is loaded */

    // Calling the jQuery plugin and splitting the
    // #holder UL into pages of 3 LIs each:

    $('#holder').sweetPages({perPage:1});

    // The default behaviour of the plugin is to insert the 
    // page links in the ul, but we need them in the main container:

    var controls = $('.swControls').detach();
    controls.appendTo('#main');

    // Make Nav Buttons

    function swGotoPage(page){
    $('.swShowPage:contains("' + page + '")').click();
}

var baseFB = '<input type="button" class="swFB" />';
var offset = 'pgOffset';
var active = '.swShowPage.active';

var $pgBack = $(baseFB)
    .attr('id', 'button_back')
    .attr('value', "Back")
    .attr(offset, '-1');

var $pgForward = $(baseFB)
    .attr('id', 'button_forward')
    .attr('value', "Next")
    .attr(offset, '1');

$.each([$pgBack, $pgForward], function(i,$obj){
    $obj.click(function(){
        var nextPage =  parseInt($(active).text(), 10) + parseInt($(this).attr(offset), 10);
        swGotoPage(nextPage);
    });
});


$($pgForward).addClass("teach_create_backforward");
$($pgBack).addClass("teach_create_backforward");

$('.teach_create_pageheader_back').prepend($pgBack);
$('.teach_create_pageheader_forward').prepend($pgForward);


});

HTML:

<li>
    <div class="noselect" id="teach_create_pageheader">
        <span id="teach_create_pageheader_back"></span>
        Step 1
        <span id="teach_create_pageheader_forward"></span>
    </div>
</li>
<li>
    <div class="noselect" id="teach_create_pageheader">
        <span id="teach_create_pageheader_back"></span>
        Step 2
        <span id="teach_create_pageheader_forward"></span>
    </div>
<li>
    <div class="noselect" id="teach_create_pageheader">
        <span id="teach_create_pageheader_back"></span>
        Step 3
        <span id="teach_create_pageheader_forward"></span>
    </div>
<li>

【问题讨论】:

  • @Daff 谢谢,不知道为什么每次格式都这么笨!

标签: jquery css class button


【解决方案1】:

元素 ID 在单个页面中应该是唯一的。

这是你想要的吗? http://jsfiddle.net/haER9/2/

像这样注册你的点击事件

$(".button_forward").click(function(){alert("forward");})
$(".button_back").click(function(){alert("back");})

【讨论】:

  • @Quincy 那么我需要做什么来解决这个问题。我对 jQuery 不是很好。
  • @Quincy 是的,但按钮现在不起作用。他们显示是的,但不要像原来那样移动页面..
  • 因为你的按钮有重复的 id,我把 button_back 和 button_forward 改成了类。您的事件应该检查​​类名而不是 id。
  • @Quincy 他们现在都是类.. 他们的按钮现在不跟随点击功能了。
  • @JD 告诉我们您是如何绑定点击事件的,或者尝试我的回答中显示的方式。
【解决方案2】:

每个 jQuery API(参见 http://api.jquery.com/id-selector/)...

每个 id 值只能使用一次 一个文档内。如果不止一个 元素已被分配相同的 ID, 使用该 ID 的查询只会 选择第一个匹配的元素 DOM。

我会避免重复使用标识符,而是在类上选择。

例如(不确定这是否适合您 - 但您明白了)...

<li>
    <div class="noselect" id="teach_create_pageheader1">
        <span class="teach_create_pageheader_back"></span>
        Step 1
        <span class="teach_create_pageheader_forward"></span>
    </div>
</li>

$('.teach_create_pageheader_back').prepend($pgBack);
$('.teach_create_pageheader_forward').prepend($pgForward);

【讨论】:

  • @Mayo ,我应该如何清理代码来做到这一点? JQ 看起来你比我好!
  • @Mayo 我已经更改了课程。现在元素无处不在,但按钮不起作用。
  • 我做了一个小提琴来作为答案发布,但我会在这里添加它,因为你已经得到它jsfiddle.net/mharen/XFCwJ
  • @Michael Haren ,这些按钮现在不起作用..但确实会出现在任何地方!
  • @jd audi 你希望这些按钮做什么?在我的小提琴中,我只是让他们发出警报。您的标记中没有任何被归类为“.swShowPage.active”的元素,所以这是错误的,或者您没有提供所有代码
猜你喜欢
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
相关资源
最近更新 更多