简体   繁体   English

jQuery单击不适用于IE11中动态添加的href

[英]jquery click not working on dynamically added hrefs in IE11

Hello I have some links in my HTML code. 您好,我的HTML代码中有一些链接。 I want to change the href property of each link on hover and then on clicking this link I want to open it up in a new tab. 我想在悬停时更改每个链接的href属性,然后单击此链接,然后在新选项卡中将其打开。 The code is as follows: 代码如下:

$('.identifierClass').hover(function(e) {
if(condition) // is true
{
    $(element).attr("href", "url/goes/here").off().click(function(e) {
        $(element).attr("target", "_blank");
    });
 }
});

Everything is working properly in Chrome/Firefox, however, on clicking the link in IE 11 it simply hangs and click wont work. 一切在Chrome / Firefox中都可以正常运行,但是,单击IE 11中的链接后,它只会挂起,然后单击将不起作用。

Any help is appreciated. 任何帮助表示赞赏。

You need to bind to a static or preexisting element that the dynamic elements will be created inside of: 您需要绑定到将在以下位置创建动态元素的静态或预先存在的元素:

$(document).on('mouseenter','.identifierClass',function(e) {
if(condition) // is true
{
    $(element).attr("href", "url/goes/here").attr("target", "_blank");
 }
});

Edit: here is a fiddle of it and I also had to use 'mouseenter' instead of 'hover' when using the string name for the event. 编辑:这是它的一个小提琴 ,当使用事件的字符串名称时,我还必须使用“ mouseenter”而不是“ hover”。 jquery .hover() documentation jQuery .hover()文档

In the fiddle i show you two divs being added dynamically: 在小提琴中,我向您展示了两个动态添加的div:

$('#place').html('<div class="identifierClass">hover1</div><div class="identifierClass2">hover2</div>');

Above that, I set my event handlers, for hover1 div, I set the event on the document using a specified selector: 除此之外,我设置了事件处理程序,对于hover1 div,我使用指定的选择器在文档上设置了事件:

$(document).on('mouseenter','.identifierClass',function(e) {
alert('hi');
});

You can see this works when you hover of 'hover1' text on the right and, conversely, you can see hover2 doesn't work using this binding: 当您将鼠标悬停在右侧的“ hover1”文本上时,您可以看到此方法有效;相反,使用此绑定,您可以看到hover2不起作用:

$('.identifierClass2').hover(function(e) {
alert('hi2');
});

here is a link to the jquery documentation on event delegation. 是有关事件委托的jquery文档的链接。

Edit2: I updated the fiddle to address the 'href' manipulation. Edit2:我更新了小提琴以解决“ href”操作。 It appears that you just want to change some attributes on the hover portion: 看来您只想更改悬停部分的某些属性:

I modified the 'mouseenter' binding to look like this: 我修改了'mouseenter'绑定,使其看起来像这样:

$(document).on('mouseenter','.identifierClass',function(e) {
    alert('hi');    $('#someLink').attr('href','http://www.bing.com').attr('target','_blank');
});

I don't think you need the 'off' or the 'click', but that is based off of some assumptions, so please feel free to comment and I can update accordingly. 我认为您不需要“关闭”或“点击”,但这是基于某些假设的,因此请随时发表评论,我会相应地进行更新。 This, though, will change the href when the mouseenters the dynamic element and change the target attribute as well. 但是,这将在鼠标输入动态元素时更改href并同时更改target属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM