1

(明確にするために質問全体を書き直さなければなりませんでした。一部の古い回答は内容と完全に一致しない場合があります)

ここでは、問題をデモするために作成された 2 つのフィドルがあります。

表の行をクリックすると、アラートが表示されます。

ただし、「詳細」ボタンをクリックしても、このアラートは表示されません。

実際の例はここに正しく示されています

ただし、この機能からプラグインを作成すると、stopPropagation() が機能しなくなり、ここに示すように、「詳細」ボタンをクリックするとアラートが表示されます。

プラグイン:

$.fn.shorten = function(settings) {

   var config = $.extend( {
     showChars : 100,
     ellipsesText : "...",
     moreText : "more",
     lessText : "less"
    }, settings);

$(document).off('click', '.morelink').on('click', '.morelink', function(e){ 
    e.preventDefault(); 
    e.stopPropagation();    

  var $this = $(this);

  // Toggle del nombre del link
  if ($this.hasClass('less')) { // clic en more para mostrar less

    $this.removeClass('less');
    $this.html(config.moreText);

    // muestro shorcontent y escondo allcontent
    $this.parent().prev().prev().show(); // shortcontent
    $this.parent().prev().hide(); // allcontent

  } else { // click en less para mostrar more

    $this.addClass('less');
    $this.html(config.lessText);

    $this.parent().prev().prev().hide(); // shortcontent
    $this.parent().prev().show(); // allcontent
  }

  return false;
});

return this.each(function() {
  var $this = $(this);

  var content = $this.html();
  if (content.length > config.showChars) {
    var c = content.substr(0, config.showChars);
    if (c.indexOf('<') >= 0) // If there's HTML don't want to cut it
    {
      var inTag = false; // I'm in a tag?
      var bag = ''; // Put the characters to be shown here
      var countChars = 0; // Current bag size
      var openTags = []; // Stack for opened tags, so I can close them later

      for (i=0; i<content.length; i++)
      {
        if (content[i] == '<' && !inTag)
        {
          inTag = true;

          // This could be "tag" or "/tag"
          tagName = content.substring(i+1, content.indexOf('>', i));

          // If its a closing tag
          if (tagName[0] == '/')
          {
            if (tagName != '/'+openTags[0]) console.log('ERROR en HTML: el tope del stack debe ser la tag que cierra');
            else
              openTags.shift(); // Pops the last tag from the open tag stack (the tag is closed in the retult HTML!)
          }
          else
          {
            // There are some nasty tags that don't have a close tag like <br/>
            if (tagName.toLowerCase() != 'br')
              openTags.unshift( tagName );// Agrega al inicio el nombre de la tag que abre
          }
        }
        if (inTag && content[i] == '>')
        {
          inTag = false;
        }

        if (inTag) bag += content[i]; // Add tag name chars to the result
        else
        {
          if (countChars < config.showChars)
          {
            bag += content[i];
            countChars ++;
          }
          else // Ya tengo los caracteres necesarios
          {
            if (openTags.length > 0) // Tengo tags sin cerrar
            {
              console.log('Quedaron tags abiertas');
              console.log(openTags);
              for (j=0; j<openTags.length; j++)
              {
                console.log('Cierro tag '+ openTags[j]);
                bag += '</'+ openTags[j] +'>'; // Cierro todas las tags que quedaron abiertas

                // You could shift the tag from the stack to check if you end with an empty stack, that means you have closed all open tags
              }
              break;
            }
          }
        }
      }
      c = bag;
    }

    var html = '<span class="shortcontent">' + c + '&nbsp;' + config.ellipsesText +
               '</span><span class="allcontent">' + content +
               '</span>&nbsp;&nbsp;<span><a href="javascript:void(0)" class="morelink badge">' + config.moreText + '</a></span>';

    $this.html(html);
    $(".allcontent").hide(); // Esconde el contenido completo para todos los textos
  }
});

};

4

3 に答える 3

3

使えるだけでなくreturn false;、 も使えますJavaScript:void(0)

両者の違いについてはこちらをご覧ください。

stopPropogation()preventDefault()は異なりますが、両方を実行return falseします。

最後.live()に減価償却、on()代わりに私たち: http://api.jquery.com/on/


あなたは試すことができます:

$('body').on('click', '.morelink', function(e) {
      e.preventDefault();
      ...
}

より永続的な解決策が見つかるまで、必要に応じて機能させるためのハックな一時的な変更を次に示します。ねえ、それは jQuery のバグかもしれませんが、わかりません。いずれにせよ、これにより、td をクリックしてアラートを実行し、ドロップダウン (more/less) をクリックしないで実行できます。

$(document).ready(function() {

    $('#tab_open_deals tbody tr td').off('click').on('click', function(e) {
    var $target = $(e.target);
    if($target.is(".morelink"))
    {
        // You clicked the morelink. Do nothing.
    }
    else
    {
        // Do your stuff!       
    }

});
于 2013-01-17T12:33:20.907 に答える
1

何のブラウザ?

これを試して:

if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
return false;
于 2013-01-17T12:18:13.860 に答える
1

href 属性 href="reurn false;" に次を追加してみてください。または、クリック イベント ハンドラ - 呼び出されている関数を見つけて、コードの最後に return false; を追加することもできます。または、Aタグを完全に別のものに変更します..

于 2013-01-17T12:20:56.473 に答える