尝试在prepend元素上添加click事件: [英] Trying to add click event on the prepend element:

查看:193
本文介绍了尝试在prepend元素上添加click事件:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在点击图片后尝试重定向到其他页面

Trying to redirect to different page on click of image

 $('.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.
});

推荐答案

首先使您的元素字符串像这样服从并追加到目标

first make your element string to obejct like this and append to target

var el = $('<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').append(el)

el.click(function(){ /* do something */ });

比创建元素的方法慢很多,但是如果要处理该元素,则很复杂.

it many slow than your method for creating element but u have complexity if want to doing with that element.

这篇关于尝试在prepend元素上添加click事件:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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