【问题标题】:Trying to add click event on the prepend element:尝试在前置元素上添加点击事件:
【发布时间】:2018-06-11 08:22:17
【问题描述】:

点击图片时尝试重定向到不同的页面

 $('.grid').prepend('<div id="new" style="width:50px;background-color:#1879A9;padding-left:3px;margin-top:10px"><a href="#somelink"><img id="test" src="#somesource"></a></div>'); 

尝试了以下代码:

$('.grid').on('click', '#test', function() {
      alert("test"); //It didnt work.
});

【问题讨论】:

  • 请添加更多详细信息。我们不知道你的 DOM。如果用户在类 grid 的元素内单击 ID 为 test 的元素,则在此处添加一个事件...
  • 请注意,您应该更改预先添加的 HTML 以使用类,而不是 id,因为您最终将附加多个具有相同 id 的元素,这是无效的。这样,您也可以使用 CSS 样式表而不是内联样式,应尽可能避免使用这种样式
  • 如果你使用&lt;a&gt; 元素和一个有效的href,那么在它上面添加一个事件监听器是没有用的。链接上的点击事件会先被触发。
  • 它按预期工作! JSFiddle,虽然不建议在使用 prepend 时使用 Id,但使用 class 而不是 ID。

标签: javascript jquery html sharepoint


【解决方案1】:

可能是这样的:

$('.grid').on('click', window.location.href = 'page_url');

【讨论】:

  • 1) 这将在用户单击 .grid 之前立即重定向 2) 需要委托事件处理程序,因为在 DOM 加载后附加内容 3) OP 没有表明他们无论如何都想重定向页面
  • 嘿嗨..“.grid”是一个内置的共享点类。我正在尝试使用图像标签将我的 div 元素添加到它前面。我想在点击该图像时重定向到另一个页面。
【解决方案2】:

首先你必须使用 id 的类 insted ,(当多个元素的 id 相同时语义错误)

还可以在单​​击图像时重定向到您的页面, 首先防止点击网格时的默认动作,所以直接点击链接时不会重定向。

然后获取父标签图像的链接并使用 $(this).parent("a").attr("href"); 以编程方式执行重定向window.location.href

参见下面的 sn-p :

$('.grid').prepend(
   '<div class="new" style="width:50px;background-color:#1879A9;padding-left:3px;margin-top:10px"><a href="https://stackoverflow.com"><img class="test" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcShuHaRYzsoiz_KpVL5Ite65oK_ILy3p1b5vrZ996D1HFKdAS64"></a></div>');
    
$('.grid').on('click', '.test', function(e) {
  e.preventDefault();
  var urlRedirect = $(this).parent("a").attr("href");
  console.log("redirecting to ..."+ urlRedirect);
  // this will redirect after 2 sec
  setTimeout(function(){
    window.location.href = urlRedirect;
  },2000)
  
  
});
.test {
  width:20px;
  height:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grid"></div>

【讨论】:

  • 这是否有用:)
【解决方案3】:

已编辑

我认为你只是创建了多个相同的 ID 元素。

使用 ID 更改为类,它应该可以正常工作。

已编辑

根据提问者的评论修改

$('.grid').each(function(event){
    if($(this).find('.test').length > 0) return false;
    var _html = '<div style="width:50px;background-color:#1879A9;padding-left:3px;margin-top:10px"><a class="link" href="'+$(this).data('url')+'"><img class="test" src="'+$(this).data('image')+'"></a></div>';
    var new_node = $(_html);
    $(this).prepend(new_node);
    new_node.find('.test').click(function(event){
        event.preventDefault();
        alert($(this).attr('src'));
        // redirect here
        // window.location.href = $(this).parent().attr('href');
    })
})
.grid{

width:40px;
height:40px;
background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grid" data-url="x.x.x" data-image="http://x.x.x"></div>
<br/>
<div class="grid" data-url="y.y.y" data-image="http://y.y.y"></div>
<br/>
<div class="grid" data-url="z.z.z" data-image="http://z.z.z"></div>
<br/>
<div class="grid" data-url="a.a.a" data-image="http://a.a.a"></div>

【讨论】:

  • 除了空格,你到底改了什么?
  • 没有变化。我觉得代码没问题,所以我只是做了一个例子。
  • 在这种情况下,这不是一个答案。如果它对 OP 不起作用,则必须存在一个无法解决的潜在问题
  • 好的。我只是认为让它工作是答案。我现在该怎么办?删除这个答案?
  • 那可能是最好的
猜你喜欢
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 2016-10-31
相关资源
最近更新 更多