我正在编写一些需要click为链接触发事件的JavaScript代码.在Internet Explorer中,我可以这样做
var button = document.getElementById('myButton');
button.click();
Run Code Online (Sandbox Code Playgroud)
但这在Firefox中不起作用,我假设任何其他浏览器.在Firefox中,我做到了这一点
var button = document.getElementById('myButton');
window.location = button.href;
Run Code Online (Sandbox Code Playgroud)
我觉得这不是最好的方法.有没有更好的方法来触发click事件?优选地,无论元素类型或浏览器如何都可以工作.
我已经使用脚本来跟踪几个月的出站链接了.该脚本为WORKS,但在Google Analytics生成的报告中,许多网址末尾都有一个尾随":80"(默认端口号).请阅读以获得更多详情.
可能很重要的是,跟踪这些出站链接的网站有大量的出站流量(将您的幻想乘以∞).
它会跟踪所有出站链接,并将其标记为Google Analytics中的"出站链接".
该脚本有很多注释,并有一些console.log()实例来帮助调试(这些实例被注释掉).
GA上的"出站链接"显示在下面:
内容>活动>热门活动>"出站链接"[点击它]> [报告显示所有点击的网址]
在"出站链接"报告中,我获得了所有点击的链接,在报告的所有链接中至少有2/3结束时得到":80"(可能更多).GA将http://example.com和http://example.com:80视为不同的链接,在报告中将它们分开.这当然不是所希望的.
值得一提:
以":80"结尾的链接总是比不具有":80"的等价物具有更多的点击量,任何点击量都会增加40%到60%.
// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
$("a").on('click',function(e){
var url = $(this).attr("href");
// Console logs shows the domain name of the link being clicked and the current window
// console.log('e.currentTarget.host: ' + e.currentTarget.host);
// console.log('window.location.host: ' + window.location.host);
// …Run Code Online (Sandbox Code Playgroud) 我想动态创建一个<a href="mailto:...">元素,然后"点击"它.所有这些都没有修改页面.
我正在尝试这个:
$('<a href="mailto:test@test.com"> </a>').click();
Run Code Online (Sandbox Code Playgroud)
......无济于事