javascript中如何判断带有超链接的图片是否被点击?

JavaScript07

javascript中如何判断带有超链接的图片是否被点击?,第1张

换一下思路就OK了,

如果你是需要在点击A链接,弹出A对应的提示,点击B链接,弹出B对应的提示,那你的代码已经可以实现:

funciton aa(type){

if(type == 1){alert("1")}

if(type == 2){alert("2")}

}

<a href="javascript:void(0)" onclick="javascript:aa(1)return false" ><img src="xingse.jpg" id="xing" width="10" height="10" alt="201003006杏色"/></a>

<a href="javascript:void(0)" onclick="javascript:aa(2)return false" ><img src="zise.jpg" id="zi" width="10" height="10" alt="201003006紫色" /></a>

如果你想在其它的代码中来实现,那需要作一个全局的JS变量

<script type="text/javascript">

var cache = 0

funciton aa(){

if(cache == 1){alert("1")}

if(cache == 2){alert("2")}

}

</script>

<a href="javascript:void(0)" onclick="javascript:cache=1return false" ><img src="xingse.jpg" id="xing" width="10" height="10" alt="201003006杏色"/></a>

<a href="javascript:void(0)" onclick="javascript:cache=2return false" ><img src="zise.jpg" id="zi" width="10" height="10" alt="201003006紫色" /></a>

<input type="botton" value="看看你刚才点了哪个" onclick="aa()">

<a id="urll" href="http://www.baidu.com/">这是一个a标签的超链接</a>

<script type="text/javascript">

 document.getElementById("urll").onclick = function(){

  //此处输入超链接点击时要执行的代码

 }

</script>

可以试试我这个,因为jQuery选择器的原因,所以一定要指定父级,否则会执行多次。

<div id="parent"><a id="a1" href="javascript:void(0)">点我</a><a id="a2" href="javascript:void(0)">点我吧</a><input id="b1" type="button" value="还是点我"></input><input id="b2" type="button" value="那点你吧"></input></div>$("#parent *").click(function(e){if(e.target == $("#a1")[0]){alert("你点了链接一!")}else if(e.target == $("#a2")[0]){alert("你点了链接二!")}else if(e.target == $("#b1")[0]){alert("你点了按钮一!")}else if(e.target == $("#b2")[0]){alert("你点了按钮二!")}}) 怎么使用jquery判断一个元素是否含有一个指定的类(class)

在jQuery中可以使用2种方法来判断一个元素是否包含一个确定的类(class)。两种方法有着相同的功能。2种方法如下:

hasClass(‘classname’)

is(‘.classname’)

以下是一个div元素是否包含一个redColor的例子:

1. 使用is(‘.classname’)的方法

$('div').is('.redColor')

2. 使用hasClass(‘classname’)的方法(注意jquery的低版本可能是hasClass(‘.classname’))

$('div').hasClass('redColor')

以下是检测一个元素是否含有一个redColor类的例子,含有时,则把其类变为blueColor。

<html>

<head>

<styletype="text/css">

.redColor {

background:red

}

.blueColor {

background:blue

}

</style>

<scripttype="text/javascript"src="jquery-1.3.2.min.js"></script>

</head>

<body>

<h1>jQuery check if an element has a certain class</h1>

<divclass="redColor">This is a div tag with class name of "redColor"</div>

<p>

<buttonid="isTest">is('.redColor')</button>

<buttonid="hasClassTest">hasClass('.redColor')</button>

<buttonid="reset">reset</button>

</p>

<scripttype="text/javascript">

$("#isTest").click(function () {

if($('div').is('.redColor')){

$('div').addClass('blueColor')

}

})

$("#hasClassTest").click(function () {

if($('div').hasClass('redColor')){

$('div').addClass('blueColor')

}

})

$("#reset").click(function () {

location.reload()

})

</script>

</body>

</html>