尝试在prepend元素上添加click事件:

编程入门 行业动态 更新时间:2024-10-27 12:25:15
本文介绍了尝试在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事件:

本文发布于:2023-11-10 23:00:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   事件   prepend   click

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!