[英]Alert is not working with anchor tag
I have created 2 anchor tag and then i created a function in which if you click the 1st anchor tag a new class will added to the 2nd one and after that when you click on the second anchor tag it will show a alert, in which its shows the text of the anchor tag with added class. 我创建了2个锚标记,然后创建了一个函数,其中,如果您单击第一个锚标记,则将一个新类添加到第二个锚标记中,然后,当您单击第二个锚标记时,它将显示一个警报,在该警报中显示添加了类的锚标记的文本。 But its not working for me.
但是它对我不起作用。
Here is the code for your reference: 这是供您参考的代码:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script>
<style type="text/css">
.highlight{background-color:yellow;}
</style>
<script>
$(function(){
$("a:eq(0)").click(function(){
$("a:eq(1)").addClass("highlight");
});
$("a.highlight").click(function(){
alert($(this).text());
});
});
</script>
</head>
<body>
<a href="#">first</a>
<a href="#">second</a>
</body>
This should be used for the second one: 这应该用于第二个:
$("a.highlight").live('click', function(){
alert($(this).text());
});
Please check this jsFiddle . 请检查此jsFiddle 。
However, if you are using jQuery 1.7 or later, you are encouraged to use on()
method. 但是,如果您使用的是jQuery 1.7或更高版本,建议您使用
on()
方法。 Please check the doc for details 请检查文档以获取详细信息
I believe the problem here is that when the script is executed there is no anchor links with the class highlight
我相信这里的问题是,执行脚本时,没有与类
highlight
锚链接
A simply fix would be to utilize the on
method in jQuery. 一个简单的解决方法是利用jQuery中的
on
方法。 Simply change the line $("a.highlight").click(function(){
只需更改
$("a.highlight").click(function(){
to this: 对此:
$("body").on("click", "a.highlight", function(){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.