简体   繁体   English

将多个链接href设置为特定链接jquery

[英]Set multiple link href to a specific link jquery

according to this: Set image src to another image jquery it says you can do that function, can it be done to links simialrly ? 根据此: 将图像src设置为另一个图像jquery,它说您可以执行该功能,是否可以同时链接?

<div id="infolink"><a href="#">See this page</a></div>

<div class="box1"><a href="www.example.com"></a></div>
<div class="box1"><a href="www.example2.com"></a></div>

Yes, so there are two links in two boxes with same class but with different links, when you click any of these boxes, the link in that box will go to the #infobox link 是的,因此在两个具有相同class但具有不同链接的框中有两个链接,当您单击其中任何一个框时,该框中的链接将转到#infobox链接

Update 更新资料

will this work : 这项工作会:

 $('.box1 ').click(function() {
var get = $('.box1 a').attr('href');
$('#infolink a').attr('href', get);}

You can prevent the default action of the click event on other two anchor tags with 您可以使用以下方法阻止click事件对其他两个锚定标记的默认操作:

e.preventDefault();

and then redirect the user to location specified by anchor tag withing <div> with id='infolink' by 然后将用户重定向到锚标签指定的位置,并使用<div>id='infolink'

location.href  = $("#infolink > a")[0].href

Javascript: Javascript:

  $(".box1 > a").on('click', function(e){
    e.preventDefault();
    location.href  = $("#infolink > a")[0].href
  })

Here the demo 这里的演示

OR 要么

You may simply change their href altogether 您可以完全更改他们的href

$(function(){

  var ele = $(".box1");

  ele.each(function(){
     $(this).children('a')[0].href = $("#infolink > a")[0].href
   })

});

From what I have understood this is what you need. 据我了解,这就是您所需要的。

$(".box1").click(function(){
    var url = $(this).find("a").attr("href"); // Get url from clicked href
    $("#infolink").find("a").attr("href", url); // set it to infolink href
    e.preventDefault(); // to prevent form submission
});

Check this: 检查一下:

$( ".box1 a" ).click(function( event ) {
 event.preventDefault();
 var get = $(this).attr('href');
 $("#infolink a").attr('href', get)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM