切换时锚没有文本更改

时间:2013-10-09 09:38:44

标签: php jquery jquery-click-event

切换时锚点没有文字更改请提供一些建议

HTML:

<a href="#" onclick="javascript:showHide('flight_<?php echo $flightlist->id ?>'); class="fnt-size12">+Flight Details</a>

<div id="flight_<?php echo $flightlist->id; ?>" class="minDetailBox addmarginB10 borT">
   <div class="col1">
      <div class="padding">
         <div class="airLogo"> <span class="airSprites LS2">&nbsp;</span>
            <p>JET Lite</p>
         </div>
      </div>
   </div>
</div>

脚本:

function showHide(id) {       
   var minDetails = document.getElementById(id);        
   $(minDetails).toggle('slow');
  ($(this).text() === "+Flight Details") ? $(this).text("-Flight Details") :  $(this).text("+Flight Details");      
   return false;
}

4 个答案:

答案 0 :(得分:2)

将您的element传递给onClick="return showHide('flight1',this)"

function showHide(id,element) {       

   var minDetails = document.getElementById(id);        

   $(minDetails).toggle('slow');

   ($(element).text() === "+Flight Details") ? 
               $(element).text("-Flight Details") :  
               $(element).text("+Flight Details");      

   return false;
}

答案 1 :(得分:0)

您可以尝试这样的事情

<a href="#" onClick="return showHide('flight1', this);" class="fnt-size12">+Flight Details</a>

function showHide(id, link) {       
    $('#' + id).toggle('slow');
    ($(link).text() === "+Flight Details") ? $(link).text("-Flight Details") : $(link).text("+Flight Details");
    return false;
}

答案 2 :(得分:0)

<a href="#" id="flight1" class="toggle fnt-size12">+Flight Details</a>

jQuery('a.toggle')
    .click(function(e) {
        var id = jQuery(this).attr('id');

        jQuery('#'+id).toggle('slow');
        if(jQuery('#'+id).css('display') == 'none') {
            jQuery(this).html("+Flight Details");
        } else {
            jQuery(this).html("-Flight Details");
        }

        e.preventDefault();
    });

答案 3 :(得分:0)

您可以按照以下方式执行此操作,

jQuery代码

$(function()
{
    $('div[id^="flight_"]').hide();
    $(document).on('click','.fnt-size12',function(e)
    {
        e.preventDefault();
        var $that = $(this);

        $(this).next('#flight1').toggle();
        if($that.html() == '+Flight Details') {
            $that.html('-Flight Details');
        } else {
            $that.html('+Flight Details');
        }

    })
});

您的HTML

<a href="#" class="fnt-size12">+Flight Details</a>

<div id="flight1" class="minDetailBox addmarginB10 borT">
   <div class="col1">
      <div class="padding">
         <div class="airLogo"> <span class="airSprites LS2">&nbsp;</span>
            <p>JET Lite</p>
         </div>
      </div>
   </div>
</div>

请参阅我修改了您的HTML code并添加了几行jQuery代码。