最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - .replace method in jquery - Stack Overflow

matteradmin6PV0评论

i want to replace class me to old when user clicks on a tag, i want to do that only with .replace function. My purpose to learn how replace method work. But the function which i have made not working.

<head>    
<script type="text/javascript" src="jquery-1.7.2.js"></script>  
<script type="text/javascript">
$(function(){
    $('a').click(function(){
        $(this).attr('class').replace('me','old')
    })
})  
</script>
</head>
<body>
<a href="#" class="me">click</a>
</body>

i want to replace class me to old when user clicks on a tag, i want to do that only with .replace function. My purpose to learn how replace method work. But the function which i have made not working.

<head>    
<script type="text/javascript" src="jquery-1.7.2.js"></script>  
<script type="text/javascript">
$(function(){
    $('a').click(function(){
        $(this).attr('class').replace('me','old')
    })
})  
</script>
</head>
<body>
<a href="#" class="me">click</a>
</body>
Share Improve this question edited Jul 2, 2012 at 3:38 bfavaretto 71.9k18 gold badges117 silver badges159 bronze badges asked Jul 2, 2012 at 3:13 JitenderJitender 7,98932 gold badges116 silver badges218 bronze badges 6
  • You can start here developer.mozilla/en/JavaScript/Reference/Global_Objects/… – elclanrs Commented Jul 2, 2012 at 3:15
  • "i want to do that only with" -- is there a real reason for this requirement? – zerkms Commented Jul 2, 2012 at 3:23
  • If you want to learn how to use .replace() do it in the right context. Adding/removing classes is really pletely unrelated to string manipulation, which is the purpose of .replace() – nbrooks Commented Jul 2, 2012 at 3:25
  • If you want to focus on String.replace, start by not using jQuery. – bfavaretto Commented Jul 2, 2012 at 3:26
  • seriously, what's the point? Just do it the natural way, and get moving along. – Zerium Commented Aug 24, 2012 at 7:23
 |  Show 1 more ment

4 Answers 4

Reset to default 3

replace is not a jQuery function it is a function of string. You can read more about replace here. To use replace, you can read the attribute as a string, replace the contents of the string and then add the attribute back.

I don't think it is the best way to do this if you have jQuery loaded. You can use the jQuery utility designed to do these manipulations, like so:

 $(this).toggleClass('me old');

This will turn on and off (toggle) both those class names. In effect it will switch from one to the other.

Docs:

http://api.jquery./toggleClass/

you are getting the class and it doesn't effect the class attribute, you can replace the string then set it to the element:

 $('a').click(function(){
    var cls = $(this).attr('class').replace('me','old')
    $(this).removeClass('me').addClass(cls)
    })
})
$('a').click(function(){
    $(this).replaceWith('<a href="#" class="old">click</a>');
});

I have created a bin with the solution on http://codebins./codes/home/4ldqpco

Try this instead:

<head>

<script type="text/javascript" src="jquery-1.7.2.js"></script>


<script type="text/javascript">


$(function(){


    $('a').click(function(){

        var theclass = $(this).attr('class');
        theclass = theclass.replace('me', 'old');
        $(this).attr('class', theclass);


        })


    })



</script>


</head>

<body>

<a href="#" class="me">click</a>

</body>

That should do the trick. JSFiddle Link

Post a comment

comment list (0)

  1. No comments so far