简体   繁体   English

根据调用锚标记来动态更改锚标记的href。Javascript或JQuery

[英]TO dynamically change the href of anchor tag according to calling anchor tag..Javascript or JQuery

Conisder anchor tags a1,a2,a3 in div1 and if a person clicks on a1 thn a div2 sud open and it contains some anchor tags with some default href value...how to change the href values for those anchor tags in div2 according to the anchors selected in div1..... 考虑div1中的锚定标记a1,a2,a3,如果有人单击div2 sud打开的a1,并且其中包含一些锚定标记和一些默认的href值...如何根据在div1 .....中选择的锚点

Example... 例...

One 1One 1Two 2One 一1一1二2一

in the above coding...we need to change the href of a3,a4,a9,a10,etc...according to the calling anchor....if calling anchor is a1...thn a3 href sud be help.html...etc...like tht...please help me... 在上面的编码中...我们需要根据调用锚点更改a3,a4,a9,a10等的href ....如果调用锚点是a1 ...那么a3 href sud会有所帮助。 html ...等...如...请帮助我...

Ok... 好...

links = new Array(
  '#link-1' : 'a_page.html',
  '#link-2' : 'another_page.html',
  '#link-3' : 'yet_another_page.html'
);

$(function () {
  $('#div1 a[href^=#link-]').click(function () {
    $('#div2').show();
    $('#div2 #a3').href(links[$(this).href()]);
  });
});

What this does is set an associative array containing the pages which correspond to the hrefs of elements in the first div. 这样做是设置一个关联数组,该数组包含与第一div中的元素的href相对应的页面。 Then when one of the of the links is clicked (matched by whether its starts with #link- the second div is revealed, and the link with id a3 is changed to match the page specified in the array. 然后,当单击其中一个链接时(匹配其是否以#link-开头-将显示第二个div,并且ID为a3的链接将更改为与数组中指定的页面匹配。

Edit: Include this in the <head> part of your site: 编辑:将其包括在网站的<head>部分中:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Then below it include the above script in <script></script> tags. 然后在其下面的<script></script>标记中包含上述脚本。 The divs need to have the id="div1" and id="div2" attributes respectively, and the link to be changed needs id="a3" for this to work. div需要分别具有id =“ div1”和id =“ div2”属性,要更改的链接需要id =“ a3”才能起作用。

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

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