2016-11-14 84 views
-2
<a class="track_this" href="http://www.google.com" target="_blank">google</a> 

WORKS:jQuery的不能识别类选择,但工作没有它

jQuery(document).ready(function($){ 
    $("a").click(function() { 
    $.post("http://example.com/clk.php",{URL:this.href}); 
    return true; 
    }); 
}); 

不起作用:

jQuery(document).ready(function($){ 
    $(".track_this").click(function() { 
    $.post("http://example.com/clk.php",{URL:this.href}); 
    return true; 
    }); 
}); 

非常沮丧。任何帮助将不胜感激。

+0

您通常希望避免类和id名称中的下划线。改为使用连字符。 – Gavin

+1

这两个代码都能正常工作..我的意思是,它被称为点击事件。 https://jsfiddle.net/lbclucascosta/8bdq6p54/ –

+0

@Gavin强调,虽然烦人的类型,是完全有效 – DelightedD0D

回答

0

我意识到这是令人难以置信的愚蠢,但我做了这样的假设:class =“track_this”实际上是传递给我的锚标记时,它实际上不是。

看来内容管理系统(Wordpress)因为我如何格式化HTML而剥离了这个类。

<a class="track_this" href="http://google.com">google</a> was stripping the class. 

<a href="http://google.com" class="track_this">google.com</a> does not strip the class 

我不知道这是为什么。感谢所有那些花时间从这个愚蠢的问题中抽出时间的人,并对我的分析中没有更彻底的道歉表示感谢。