简体   繁体   English

用鼠标单击时如何更改锚文本?

[英]How to change the text of an anchor when clicked with the mouse?

This is my website. 这是我的网站。 http://www.sarahjanetrading.com/js/j/index.html All the code+images+css is there with access to anyone http://www.sarahjanetrading.com/js/j/index.html所有的code + images + css都可以访问

When anyone clicks on the listen button its background changes into the stop listening and vice versa. 当任何人单击“监听”按钮时,其背景将变为停止监听,反之亦然。 This is the functionality I wanted and I got it using jQuery. 这是我想要的功能,我使用jQuery获得了它。

What I also want now is the text to change too accordingly. 我现在还希望文本也相应地更改。 Like, when someone clicks on the "listen" anchor, its text should change to "stop listening", and the same for the stop listening anchor. 就像,当某人单击“收听”锚点时,其文本应更改为“停止收听”,停止收听锚点的文本也应更改为“停止收听”。 Like a toggle anchor. 像拨动锚一样。

Thanks for the help ! 谢谢您的帮助 ! Really appreciate it... :) 真的很感激... :)

$("#first").toggle(function(){
    $(this).text("Stop listening");
}, function(){
    $(this).text("Listen");
});

Online demo: http://jsfiddle.net/5AUdJ/ 在线演示: http : //jsfiddle.net/5AUdJ/


UDPATE UDPATE

I see you are using a class to control the listen/stop state. 我看到您正在使用一个类来控制侦听/停止状态。 Maybe this will work better. 也许这会更好。

$("a").click(function(){
    if($(this).is(".listen")){
        $(this).text("Stop listening").removeClass("listen");
    } else {
        $(this).text("Listen").addClass("listen");
    }
});

Here, I made this JSFiddle using your source code. 在这里,我使用您的源代码制作了这个JSFiddle。

http://jsfiddle.net/K4Njj/2/ http://jsfiddle.net/K4Njj/2/

Here is the jQuery code: 这是jQuery代码:

$(function(){
    $("#container a").click(function(){
        if ($(this).html() == "Stop Listening")
        {
            $(this).html("Listen");
        }
        else if ($(this).html() == "Listen")
        {
            $(this).html("Stop Listening");
        }
    });
});

That will simply check to see if the text on the anchor is "Stop Listening" or "Listen" and if it is, it will switch it when you hit the button. 这将仅检查锚点上的文本是“停止收听”还是“侦听”,如果是,则在您单击按钮时会切换它。 It's really the most elegant solution. 这确实是最优雅的解决方案。

在Javascript中,您可以使用innerHTML属性访问和/或更改元素的HTML文本。

getElementById("first").innerHTML = "newtext"

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

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