jQuery .click()在每个浏览器但Safari [英] jQuery .click() works on every browser but Safari

查看:801
本文介绍了jQuery .click()在每个浏览器但Safari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块JavaScript在现有div中动态创建 A 标签,然后调用jQuery函数click。

I have a piece of JavaScript that dynamically creates an A tag inside of an existing div and then calls the jQuery function "click" on it. This works as intended in all browsers except Safari.

Safari会返回以下错误:

Safari returns the following error:


'undefined'不是函数(评估'$('。shell a')[0] .click()')

'undefined' is not a function (evaluating '$('.shell a')[0].click()')

任何想法,我做错了或为什么这不工作在Safari和如何让它工作(或一块代码在所有浏览器中工作)我试过使用 .trigger(click)也会出现相同的错误。

Any ideas on what I'm doing wrong or why this doesn't work in Safari and how to get it to work (or one piece of code that works in all browsers) I've tried using .trigger("click") as well and it gives the same error.

function writeAndClickLink(url) {

    $('.shell').html('<a href="' + url + '"></a>');
    $('.shell a')[0].click();
}

并在HTML中:

<div class="shell"></div>

我这么奇怪的做法的原因是该函数是从Flash网站调用打开一个twitter窗口,我给予奖励的tweeting。这是迄今为止唯一的方法,我可以得到tweet事件绑定到窗口。如果我以任何其他方式打开窗口(例如window.open),它不捕获绑定事件,让我知道他们实际完成了tweet。

The reason I'm doing it this weird way is the function is called from a Flash site to open a twitter window where I am giving a reward for tweeting. This so far has been the only way I could get the "tweet" event to bind to the window. If I open the window any other way (window.open for example) it doesn't capture the bound event that lets me know they actually completed the tweet.


  • 用户点击flash中的tweet按钮

  • Flash调用javascript函数动态写入href并点击它

  • Twitter窗口打开

  • 用户成功tweets


  • User clicks tweet button in flash
  • Flash calls javascript function to dynamically write href and "click" it
  • Twitter window opens
  • User successfully tweets
  • Tweet event is captured and updates flash with success
  • Content is unlocked in flash

推荐答案

var a = $('.shell a')[0];
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent('click', true, true, window);
a.dispatchEvent(evObj);

请参阅http://www.howtocreate.co.uk/tutorials/javascript/domevents (搜索手动触发事件)。

See http://www.howtocreate.co.uk/tutorials/javascript/domevents (search for "Manually firing events").

这篇关于jQuery .click()在每个浏览器但Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆