English 中文(简体)
混合身份证明书
原标题:JQuery functions with hybrid ID

下面的编码是(id +_pan) 选择人的遗体。 我认为,这将奏效,但显然不是。 如何做到这一点。

  function panel(id) {
var toggle = id +  _pan ;
        if ($(id).html( Learn more » )) {
        $(id).html( Close panel « );
        $(toggle).slideDown( 600 ); }
        else if ($(id).html( Close panel « )) {
        $(toggle).slideUp( 600 );
        $(id).html( Learn more » ) }
    }

<a id="TABPAN_padi_training" href="#" onclick="panel(this); return false">Learn more &raquo;</a>

任何想法?

Marvellous

最佳回答

你实际上没有在有条件的发言中作任何比较。 你撰写的两种条件都将是真实的,因为它们正在设置超文本(当然,第二个条件从来不会受到打击,因为第一个条件永远不会恢复真实)。

此外,在身份证上没有通过小组,它通过的是实际的超文本标语, l子也会变成 j子。

你想照此写以下条件:

function panel(id) {
    var toggle = $( # +$(id).attr( id )+ _pan );
    if ($(id).html() ==  Learn more &raquo; ) {
    $(id).html( Close panel &laquo; );
    toggle.slideDown( 600 ); }
    else if ($(id).html() ==  Close panel &laquo; ) {
    toggle.slideUp( 600 );
    $(id).html( Learn more &raquo; ) }
}
问题回答

HTML

<a id="TABPAN_padi_training" href="#">Learn more &raquo;</a>

JavaScript

$(function (){
    function panel() {
        var $elt = this,
            $toggle = $(this.id +  _pan ),
            toggleIsVisible = $toggle.is( :visible ),
            CLOSE_PANEL =  Close panel &laquo; ,
            LEARN_MORE =  Learn more &raquo; ,
            slideTime = 600;
        if (toggleIsVisible) {
            $elt.html(LEARN_MORE);
            $toggle.slideUp(slideTime);
        }
        else {
            $elt.html(CLOSE_PANEL);
            $toggle.slideDown(slideTime);
        }
        return false;
    }

    $( #TABPAN_padi_training ).click(panel);
});

Code cleanup:

  • Cache your jQuery objects
  • Write if statements so they re testing the right thing - testing against the HTML is brittle, and not what you mean not to mention the fact that you were testing against the value returned by a jQuery setter, not a getter
  • Extract repeated strings into local variables to reduce chances of bugs due to typos
  • Stop using inline event handlers and start writing unobtrusive JavaScript
  • Pass a number, not a string, to .slideUp() and .slideDown(), so the argument is interpreted as a millisecond value

onclick=“panel(this)”将发送一个DOM物体,而不是一个ID。 你们需要:

<代码>var toggle = # + id.id +_pan

页: 1

function panel( obj ) {
    var selector =  #  + obj.id,
        $this = $( selector );
    if ($this.html() ==  Learn more &raquo; ) {
      $this.html( Close panel &laquo; );
      $(toggle).slideDown( 600 ); // I assume toggle is a global variable assigned elsewhere
    } else if ($this.html() ==  Close panel &laquo; ) {
      $(toggle).slideUp( 600 );
      $this.html( Learn more &raquo; ) 
    }
}

通知一还更改了您的条件,即:$.html()=内容,以便适当进行测试。

修改你的法典,你没有正确使用。

function panel(id) {
        //id is the WHOLE DOM ELEMENT
        if ($(id).html( Learn more &raquo; )) {
        $(id).html( Close panel &laquo; );
        $(toggle).slideDown( 600 ); }
        else if ($(id).html( Close panel &laquo; )) {
        $(toggle).slideUp( 600 );
        $(id).html( Learn more &raquo; ) }
    }

<a id="TABPAN_padi_training" href="#" onclick="panel(this); return false">
  Learn more &raquo;
</a>




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签