开发者

jquery ::: replace hyperlink text?

Just trying to replace the hyperlink text and I'm not finding a way to do this... I tried the code below, but, I get a syntax error?

EDIT

<script type="te开发者_StackOverflow中文版xt/javascript">
    var $j = jQuery.noConflict();
    $j(function() {
        if($j("a").text() == "Contact") {
            $j("a").text() = "Connect");
        }
    });
</script>


You have one additional bracket and the assignment is wrong.

if($j("a").text() == 'Contact') {
   $j("a").text('Connect');
}

instead of

if($j("a").text() == 'Contact')) {
   $j("a").text() == 'Connect');
}

Looks like you just copied and pasted the code from the condition and changed Contact to Connect. Be careful with that.


var $j = jQuery.noConflict();
 $j(function() {
  if($j("a").text() == 'Contact') {
   $j("a").text('Connect');
  }

 });

The above fixes your syntax problem, but on the face of it the code only makes sense if there is actually only one link present - $j('a') will select all links in the DOM! What you probably want is:

 $j(function() {
  //Replace hyperlinked 'Contact' texts with 'Connect' text
  $j("a").each(function(){
   if( $j(this).text() == 'Contact' ){
    $j(this).text('Connect');
   }
  });


You don't assign the text to the method result, you use it as a parameter to the method:

var $j = jQuery.noConflict();

 $j(function() {

  if ($j("a").text() == 'Contact') {
   $j("a").text('Connect');
  }

 });


Couldn't you just use the contains method on its own?

$("a:contains('Contact')").text('Connect');

I assume the noconflict function is being used due to multiple jQuery versions being called in the page?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜