Javascript 如何在jquery插件中停止Propagation()?(更新)
为了澄清这个问题,我不得不重写整个问题 旧答案可能与内容不完全匹配 我这里有两个小提琴用来演示这个问题 单击表格行时,将显示警报 但是,单击“更多”按钮时不应显示此警报 正确地说明了工作示例 但是,当我使用此功能制作插件时,StopRopagation不再工作,当我单击“更多”按钮时,仍然会显示警告,如图所示 插件:Javascript 如何在jquery插件中停止Propagation()?(更新),javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,为了澄清这个问题,我不得不重写整个问题 旧答案可能与内容不完全匹配 我这里有两个小提琴用来演示这个问题 单击表格行时,将显示警报 但是,单击“更多”按钮时不应显示此警报 正确地说明了工作示例 但是,当我使用此功能制作插件时,StopRopagation不再工作,当我单击“更多”按钮时,仍然会显示警告,如图所示 插件: $.fn.shorten = function(settings) { var config = $.extend( { showChars : 100,
$.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
}
});
}) 什么浏览器
试试这个:
if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
return false;
什么浏览器
试试这个:
if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
return false;
您可以尝试在href属性href=reurn false上添加以下内容:;
或者您可以找到正在调用的click eventhandler函数,并在代码末尾添加return false;或者将标签完全更改为其他内容。您可以尝试在href属性href=reurn false上添加以下内容; 或者您可以找到正在调用的click eventhandler函数,并在代码末尾添加return false;或者将标签完全更改为其他内容。不仅可以使用return false;但您也可以使用JavaScript:void0 尽管stopPropogation和preventDefault不同,但return false同时执行这两种操作 最后,生活被贬值,我们继续生活: 你可以试试:
$('body').on('click', '.morelink', function(e) {
e.preventDefault();
...
}
这里有一个骇人的临时更改,让它按照您想要的方式工作,直到您找到一个更持久的解决方案。嘿,它甚至可能是一个jQuery错误,我不知道。无论哪种方式,这都允许单击td来执行警报,而单击下拉列表或多或少地不执行警报
$(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!
}
});
不仅可以使用return false;但您也可以使用JavaScript:void0
尽管stopPropogation和preventDefault不同,但return false同时执行这两种操作
最后,生活被贬值,我们继续生活:
你可以试试:
$('body').on('click', '.morelink', function(e) {
e.preventDefault();
...
}
这里有一个骇人的临时更改,让它按照您想要的方式工作,直到您找到一个更持久的解决方案。嘿,它甚至可能是一个jQuery错误,我不知道。无论哪种方式,这都允许单击td来执行警报,而单击下拉列表或多或少地不执行警报
$(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!
}
});
.live已被弃用。请注意。jQuery 1.9已经删除了live方法,因此我将开始在上使用该方法。插件中必须有回调函数…使用那里的代码。啊,我明白了。我只是做了一个.off'click'。on'click'?不,$document.on'click','.morelink',function{e.preventDefault;e.stopPropagation;}.live被弃用了。请注意。jQuery 1.9已经删除了live方法,因此我将开始在上使用该方法。插件中必须有回调函数…使用那里的代码。啊,我明白了。我只是做一个.off'click'。on'click'?不,$document.on'click','morelink',function{e.preventDefault;e.stopPropagation;}stopPropagation和preventDefault是两个完全不同的东西。然后你可能还需要添加event.cancelBubble=true和event.returnValue=false谢谢,我已经用两个小提琴澄清了这个问题。请参阅更新的问题。stopPropagation和preventDefault是两个完全不同的东西。然后您可能还需要添加event.cancelBubble=true和event.returnValue=false谢谢,我已经用两个小提琴澄清了这个问题。请看更新的问题。谢谢Jimbo,我已经用两把小提琴澄清了这个问题。请看更新的问题。@Kave我做了一些可能很糟糕的事情。。基本上,在继续警报之前,检查您是否没有单击“更多/更少”下拉列表。这是有用的还是不是你想要的?是的,总比没有好。似乎没有其他人有任何其他想法。lol:我很惊讶没有真正的solution@Kave这很糟糕,但允许您继续使用代码,直到找到更持久的解决方案。谢谢您的帮助+从我这里得到1。不过,在找到合适的解决方案之前,我会一直保持开放状态。谢谢Hanks Jimbo,我已经用两把小提琴澄清了这个问题。请看更新的问题。@Kave我做了一些可能很糟糕的事情。。基本上,在继续警报之前,检查您是否没有单击“更多/更少”下拉列表。这是有用的还是不是你想要的?是的,总比没有好。似乎没有其他人有任何其他想法。lol:我很惊讶没有真正的solution@Kave这很糟糕,但允许您继续使用代码,直到找到更持久的解决方案。谢谢您的帮助+从我这里得到1。不过,在找到合适的解决方案之前,我会一直保持开放状态。谢谢谢谢,我已经用两把小提琴把问题弄清楚了。请看更新的问题。谢谢,我已经用两把小提琴澄清了这个问题。请参阅更新的问题。