简体   繁体   English

如何通过选择类来使用jquery更改锚标记

[英]how to change an anchor tag using jquery by selecting its class

I know this may seem really simple but am having trouble this end figuring out a way of selecting the class and changing ONLY that href.. here is the html ... 我知道这可能看起来很简单,但是我很难找到一种方法来选择类并且只改变href ..这里是html ...

<a href="http://www.something" class="theclassname">LEARN MORE</a>

so far with javascript ... 到目前为止用javascript ...

$('.theclassname').click(function(){
    (change the href to www.awesome.com)
});

So far really appreciate alll answers on this ... I need the href to change but not go to the url that it has changed to until the user clicks once more... so far the actual code is this ... 到目前为止真的非常感谢alll的答案...我需要改变href而不是去它已经改变的url,直到用户再次点击...到目前为止,实际的代码是这个......

$('.hrefclass').click(function () {
    e.preventDefault();
    $(this).attr("href", "http://www.google.org/");
    $('.kwicks').kwicks({
        maxSize: '100%',
        behavior: 'menu'
    });
});

however this does not appear to work . 但这似乎不起作用。

LATEST 最新

Ok so adding return false indeed works but also stops the kiwcks function from running ... how can i get around this ? 好吧所以添加return false确实有效,但也阻止kiwcks函数运行...我怎么能绕过这个?

$('.hrefclassname').one("click", function() {
$('.kwicks').kwicks({
maxSize: '100%',
behavior: 'menu'
    });
    $(this).attr("href", "http://www.gooogle.org/");
return false;
});

LATEST LATEST 最新的

$('.HREFCLASSNAME').one("click", function() {
 e.preventDefault();
    $(this).attr("href", "http://www.GOOGLE.org/");
$('.kwicks').kwicks({
maxSize: '100%',
behavior: 'menu'
    });

});

采用

$(this).attr('href','www.awesome.com')

You can use the attr() method of jQuery to alter the links. 您可以使用jQuery的attr()方法来更改链接。 href is the attribute here and the link is its value. href是此处的属性,链接是其值。

 $('.theclassname').on('click', function() { $(this).attr('href', 'http://google.com'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="http://www.something" class="theclassname">LEARN MORE</a> 

In your function, the variable "this" contains the DOM element that you clicked on. 在您的函数中,变量“this”包含您单击的DOM元素。 Wrap it using jQuery using " $(this) ", then call jQuery's attr function to set the href attribute to whatever url you'd like. 使用jQuery使用“ $(this) ”包装它,然后调用jQuery的attr函数href属性设置为你想要的任何url。

Here is a working jsFiddle: https://jsfiddle.net/57foqwaq/2/ 这是一个有效的jsFiddle: https ://jsfiddle.net/57foqwaq/2/

Here's the example code: 这是示例代码:

$('.theclassname').one("click", function(e) {
    e.preventDefault();
    $(this).attr("href", "https://www.example.com/");
});

If you want an invocation to a link of theclassname when it is clicked, and then to show the text and URL of the link you can try this other option: 如果你想调用到的链接theclassname当它被点击,然后告诉你可以试试这个另一种选择链接的文字和网址:

HTML HTML

<a rel='1' class="theclassname" href="option1.html">Option 1</a>
<a rel='2' class="theclassname" href="option2.html">Option 2</a>

jQuery jQuery的

$('.theclassname').one('click', function() {
   var link = $(this);
   alert (link.html());
   alert (link.attr('href'));
   alert (link.attr('rel'));

   return false;
});

The return false prevents the link from being followed. 返回false会阻止链接被跟踪。

The following code (as others have mentioned) should work: 以下代码(正如其他人提到的)应该有效:

// notice "e" being passed to the function:
$('.HREFCLASSNAME').one("click", function(e) {

    e.preventDefault();

    // assuming "this" is an "<a>" tag: 
    $(this).attr("href", "http://www.GOOGLE.org/");
    $('.kwicks').kwicks({
         maxSize: '100%',
         behavior: 'menu'
    });
});

This should stop the "event propagation," or in other words the link should not fire. 这应该停止“事件传播”,换句话说,链接不应该触发。

If this the link is still firing, you may have an issue similar to this example where the event is "bubbling up" and causing the link to fire anyway. 如果此链接仍在触发,则可能存在类似于此示例的问题,其中事件是“冒泡”并导致链接无论如何都要触发。 If this is the case (which, would indicate you haven't provided a valid html example for your specific problem,) you can stop the event from propagating by replacing e.preventDefault() with this: 如果是这种情况(表示您没有为特定问题提供有效的html示例),则可以通过将e.preventDefault()替换为e.preventDefault()来阻止事件传播:

e.stopImmediatePropagation(); 

You just need to try: 你只需要尝试:

$(this).attr("href", new_href);

And I think it might work for you. 而且我认为它可能适合你。

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

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