jQuery ATTR funk in IE
jQuery ATTR funk in IE
我正在动态创建javascript,并使用$(document).ready()将其附加到页面上链接的onclick事件
我的onclick javascript用于生成事件函数,我将这些函数传递给Google Analytics,以跟踪页面上的事件,例如点击横幅和下载的PDF。我在谷歌分析中从除IE之外的所有浏览器获取这些事件的统计数据。因此,我在IE中的代码出现了问题(我在页面上搜索了错误,没有)。
通常我会做一些类似$("a")的事情。点击…做一些事情。。。但不管出于什么原因,我能让谷歌分析事件跟踪工作的唯一方法就是直接在我的链接上设置跟踪事件功能。因此,我使用以下代码在页面加载后将跟踪事件函数注入到链接的onclick中。。。。
// Tracks favorites on the home page.
$("._gatHomePageFavorites").each
(
function(index)
{
var description = "Content ID: " + getParameterNamedID($(this).attr("href")) + " - " + $(this).children().first().attr("alt");
$(this).attr("onclick","alert('1');_gaq.push(['_trackEvent', 'Favorites - Home Page', 'Icon Click','" + _gatCleanString(description) + "']);alert('2');");
}
);
我认为我的问题是IE没有把我的代码放在onclick上。但我不知道在IE中查看生成源代码的好方法。我在地址栏中尝试了几个javascript函数来显示生成的源代码,假设它们有效,那么我的代码并没有将跟踪事件函数注入到我的链接的onclick for IE中。我在Firefox的视图生成源代码中的onclicck中看到了跟踪事件函数。
作为另一个测试,您可以看到我在跟踪事件功能周围添加了警报。在FF中两个警报都会触发,在IE中两个警告都不会触发。
还有一条信息。我的谷歌分析没有记录任何IE浏览器的事件。据我所知,我的问题并不是特定于某个版本的IE。
我如何判断我的动态javascript是否进入了IE的onclick,然后我该怎么做才能将其进入IE的onclick?
更新为了简化问题并集中答案的方向,我删除了谷歌分析事件功能。现在,我所做的就是将alert()注入到onlick中。IE甚至不会触发alert()。我试过以下。。。
// Tracks favorites on the home page.
$("._gatHomePageFavorites").each
(
function(index)
{
$(this).attr("onclick","alert('1')");
}
);
和
// Tracks favorites on the home page.
$("._gatHomePageFavorites").each
(
function(index)
{
$(this).attr("onclick","setTimeout('"alert('1')'", 1000);return false");
}
);
所以我仍然倾向于我的javascript没有被注入IE中的onclick。
在IE中查看生成的源代码最可靠的方法是什么
如果我能确认我的代码没有被注入到链接的onclick属性中,那么我至少可以回答为什么Google Analytics没有跟踪IE的事件。这可能是因为我注入的代码不存在于IE中。
您不应该添加"onclick"属性,而应该使用jQuery .click()
事件来使用它。
function(index){
var description = "Content ID: " + getParameterNamedID($(this).attr("href")) + " - " + $(this).children().first().attr("alt");
$(this).click(function() {
alert('1');
_gaq.push(['_trackEvent', 'Favorites - Home Page', 'Icon Click', _gatCleanString(description)]);
alert('2');
});
}
像上面这样的东西,抱歉写得这么快,所以可能有打字错误。
机会很小,但如果您的页面上有任何VBScript,那么您应该在onclick字符串前加上"javascript:"
您可以只使用历史悠久的DOM0 onclick
属性,尽管jQuery的click()
方法确实没有什么好理由不起作用。
$("._gatHomePageFavorites").each
(
function(index)
{
var $this = $(this);
var description = "Content ID: "
+ getParameterNamedID($this.attr("href")) + " - "
+ $this.children().first().attr("alt");
this.onclick = function() {
alert('1');
_gaq.push(['_trackEvent', 'Favorites - Home Page', 'Icon Click',
_gatCleanString(description)]);
alert('2');
};
}
);
jQuery的attr()
方法通常并不有用,但这是一个完全不同的rant。
跟踪链接需要注意的是,如果浏览器在获取跟踪像素之前离开页面,则可能不会记录事件,具体取决于浏览器、计算机和;网络速度。我在以下方面运气很好(从@Tim Down's
代码修改而来):
$("._gatHomePageFavorites").each
(
function(index)
{
var $this = $(this);
var description = "Content ID: "
+ getParameterNamedID($this.attr("href")) + " - "
+ $this.children().first().attr("alt");
$this.click(function(e) {
alert('1');
_gaq.push(['_trackEvent', 'Favorites - Home Page', 'Icon Click',
_gatCleanString(description)]);
if ($this.attr('target') != '_blank') {
e.preventDefault();
setTimeout('document.location = "' + $this.attr('href') + '"', 150);
}
});
}
);
从本质上讲,如果链接没有在新窗口中打开,请等待并自己关注它。
事实证明,我的问题与谷歌分析或它的跟踪事件无关。经过大量测试,我最终确定在IE中,jQuery的$().attr()不起作用。
奇怪的是,它没有断开或出现错误。IE只是以某种方式忽略了它,并且不会将我的动态javascript添加到锚标记的onclick参数中。
解决方案。。。
-
显而易见的是,要像每个人建议的那样,与事件绑定。如果你可以使用$().click(),那么我同意,永远使用它。永远不要使用$().attr("onclick","do something");
-
然而,我的项目中的其他情况使我无法使用$().click()。我无法使用谷歌分析的事件跟踪功能将统计数据输入谷歌分析,除非事件跟踪功能在锚标记上内联。
上了老派。。。
$(".obj").each
(
function (index) {
this.setAttribute("onclick", "alert('It is Working');");
}
);
感谢大家的帮助。
- window.opener not working in chrome & IE
- angularjs formly submit bug in safari & IE 11
- XMLHttpRequest in IE-7
- execScript try/catch in IE 8 中的怪异之处
- FireEvent in IE 11/Firefox
- InvalidStateError with msSaveBlob for json file in IE
- XMLHttpRequest in IE 8-9 with header
- .hasOwnProperty('getComputedStyle') false in IE 11
- Ajax call in Internet Explorer IE 7 & IE8 error
- AJAX: responseXML is null in IE 11
- Prevent fouc in firefox and ie
- jQuery ATTR funk in IE
- javascript in IE递归->window.frames不等于它自己
- 美元(文档).ready don't loading in IE
- File Object in IE 10
- JavaScript inside 'a' href in IE
- “this"in javascript在ie浏览器中的行为不同
- Moment.js YYYY-M-DD Invalid IN IE
- window.open() in IE 9
- internet explorer - JavaScript Object.create in old IE