使用 jQuery 单击 IE6 和 FF 中的 A 标签

发布于 2024-08-17 05:40:49 字数 858 浏览 5 评论 0原文

我有一个从 ajax 调用返回的 div,其中包含一个 a。我需要在 javascript 中单击它,但是我找不到在 IE6 和 FF 中都有效的方法。

这在 FF 中有效,但在 IE6 中生成 需要对象 错误:

$('#mylink').click();

这在 IE6 中有效,但生成 $("#mylink").get(0).click 不是函数 FF 中的错误。

$('#mylink').get(0).click();

关于为什么会出现这种情况以及可用的解决方案有什么想法吗?

编辑:

使用触发器返回与 IE6 中单击相同的错误:

$('#mylink').trigger('click');

编辑:

将代码放入计时器中不会更改行为:

 setTimeout(function() {
  $('#mylink').click();
 }, 100);

编辑:

作为解决方法,此功能。但如果能更好地理解这个问题就好了。这不仅仅是 jQuery 的问题(或者根本不是)。 IE6 JavaScript 错误来自 MicrosoftAjax.js,因此与此有关。

 var anchor = $('#mylink');
 if (anchor.get(0).click) {
  anchor.get(0).click();
 }
 else {
  anchor.click();
 }

I have a div that is returned from an ajax call which contains an a. I need to click it in javascript, however I cannot find a way that works in both IE6 and FF.

This works in FF but generates an object required error in IE6:

$('#mylink').click();

This works in IE6 but generates a $("#mylink").get(0).click is not a function error in FF.

$('#mylink').get(0).click();

Any ideas on why this is and what kind of solution is available?

EDIT:

Using trigger returns the same error as click in IE6:

$('#mylink').trigger('click');

EDIT:

Placing the code in a timer does not change the behavior:

 setTimeout(function() {
  $('#mylink').click();
 }, 100);

EDIT:

As a workaround, this functions. But it would be nice to better understand the issue. This is not a jQuery issue alone (or maybe at all). The IE6 JavaScript error comes out of MicrosoftAjax.js so it has something to do with that.

 var anchor = $('#mylink');
 if (anchor.get(0).click) {
  anchor.get(0).click();
 }
 else {
  anchor.click();
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画中仙 2024-08-24 05:40:49

get 方法返回 DOM 元素。
你应该使用 eq 代替。

$('#mylink').eq(0).click();

The get method returns the DOM element.
You shoudl use eq instead.

$('#mylink').eq(0).click();
无敌元气妹 2024-08-24 05:40:49

如果 $("#mylink").click() 未找到,但 $("#mylink").get(0).click() 找到,那么您可以使用它作为测试的基础吗?

例如,

if ($("#mylink").click)
{
    $("#mylink").click()
}
elseif ($("#mylink").get(0))
{
    $("#mylink").get(0).click();
}

我知道远非理想,但这就是处理 IE6 时的情况

If $("#mylink").click() isn't found but $("#mylink").get(0).click() is, then could you use this as the basis for a test?

eg

if ($("#mylink").click)
{
    $("#mylink").click()
}
elseif ($("#mylink").get(0))
{
    $("#mylink").get(0).click();
}

Far from ideal I know but such is the way of things when dealing with IE6

心在旅行 2024-08-24 05:40:49

使用 触发器 怎么样?

$("#mylink").trigger('click');

How about using a trigger ?

$("#mylink").trigger('click');
纵山崖 2024-08-24 05:40:49

尝试:

$('#mylink').trigger('click');

应该与您的第一个示例相同...在 IE 中执行一些警报以确保该元素存在等等(也许您在某处或其他地方有重复的 ID?)。

Try:

$('#mylink').trigger('click');

Should be the same as your first example, though... Do some alerts in IE to make sure the element exists and all that (maybe you have a duplicated ID somewhere or something?).

戒ㄋ 2024-08-24 05:40:49

object required 可能是因为 IE 无法及时找到 #mylink 选择器而生成的。确保在成功回调函数中进行调用或提供超时函数来在触发单击之前检查元素是否可用:

window.setTimeout(function() {
    if ($("#mylink").length) {
        $("#mylink").trigger('click');
        return false;
    }
    window.setTimeout(arguments.callee, 1);
},1);

The object required is likely generated because IE can't find the #mylink selector in time. Make sure you do the call in the success callback function or provide a timeout function that checks if the element is available before triggering the click:

window.setTimeout(function() {
    if ($("#mylink").length) {
        $("#mylink").trigger('click');
        return false;
    }
    window.setTimeout(arguments.callee, 1);
},1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文