如何用不同的<a href="">
how to call jquery function with different <a href="">
我想调用两个不同的jquery函数来隐藏链接
<a href="">.zip file, first link
</a>
<script>
$("a").click(function() {
location.href="first.location";
return false;
});
</script>
<a href="">.tar.gz file, second link
</a>
<script>
$("a").click(function() {
location.href=="second.location";
return false;
});
</script>
如何调用这两个函数,以便调用单击第一个链接的第一个函数和单击第二个链接的第二个函数?
感谢lo
这不是最好的解决方案。为了获得最佳效果,您可能需要重新构造html,并向链接或其父级添加某种类和ID来识别它们。但这将工作
对于第一个链路
$("a:eq(0)").click(function() {
location.href="first.location";
return false;
});
并且对于第二链路
$("a:eq(1)").click(function() {
location.href=="second.location";
return false;
});
如果在标记中设置href
,则不需要JQuery
或Javascript
。
<a href="first.location">.zip file, first link
</a>
<a href="second.location">.tar.gz file, second link
</a>
您可以在此处使用:eq()选择器,如:
// Clicking the first link
$("a:eq(0)").click(function () {
location.href = "first.location";
return false;
});
// Clicking the second link
$("a:eq(1)").click(function () {
location.href = "second.location";
return false;
});
就像已经有人建议的那样,最好的方法是为这些a标记使用不同的id。但是,如果出于某种原因你不想分配id(你到底为什么要这样做?)你可以做以下事情:
将锚标签包装在div中,并给它一个类似的id
<div id="myDiv">
<a href="#">First Link</a>
<a href="#">Second Div</a>
</div >
然后使用jQuery进行链接:
<script>
$(function(){
$("myDiv").children(a:first-child).click(function(){
// Do stuff here
});
$("myDiv").children(a:last-child).click(function(){
// Do stuff here
});
});
</script>
您可以在链接中引入一个id属性。然后基于元素的id触发事件。
<a href="" id='link1'>.zip file, first link
</a>
<script>
$("#link1").click(function() {
location.href="first.location";
return false;
});
</script>
<a href="" id='link2'>.tar.gz file, second link
</a>
<script>
$("#link2").click(function() {
location.href=="second.location";
return false;
});
</script>
在html(href)中提供链接
$("a").click(function()
{
location.href = $(this).attr('href');
return false;
});
我认为这可能会有所帮助:
<a id="first" href="">.zip file, first link</a>
<script>
$("first").click(function() {
location.href="first.location";
return false;
});
</script>
<a id="second" href="">.tar.gz file, second link </a>
<script>
$("second").click(function() {
location.href=="second.location";
return false;
});
</script>
$("a:eq(0)").click(function() {
location.href="first.location";
return false;
});
$("a:eq(1)").click(function() {
location.href=="second.location";
return false;
});
相关文章:
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 如何提取“;href"最近列表项中的属性值
- CKEditor如何允许href="javascript:void(0)"在小部件中
- 如何使用Javascript获取当前页面URL,并在<a href="[the_URL]”>
- JSP<a href="">无法保存常规语句
- jQuery:使用.attr('href',")调用javascript
- Linkify/Clickable Text URL,但忽略那些已经包装在“;a href"'s
- 阻止href="#"链接更改URL哈希
- a href="上的javascript对象对象;javascript:"呼叫
- "href"JavaScript Android中的链接不起作用
- 菜单href="#pannel”;不适用于android
- 参考“;href"而不是html中锚点标签中的url
- 更改URL而不使用<a href="">
- 冷融合:空链接href="#"错误
- 如何检查带有href="某事“;与Jquery有一定的类
- 突出显示菜单中具有href="#"“选项”;a“;标签
- jQuery替换所有href=""其中onclick=“;window.location=“;
- <a href="#">最终调用Javascript函数
- 命令“;window.location.href"不适用于HTML(引导框架)
- href对页面的HTML调用"UP”;目录