(我不得不重写整个问题以澄清它,一些较旧的答案可能与内容不完全匹配)
我在这里创建了两个小提琴来演示这个问题。
当您单击表格行时,将显示警报。
但是,单击“更多”按钮时不应显示此警报。
工作示例在此处正确说明
但是,当我使用此功能制作插件时,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 + ' ' + config.ellipsesText +
'</span><span class="allcontent">' + content +
'</span> <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
}
});
};