最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery how to add onclick in href - Stack Overflow

programmeradmin1浏览0评论

how to add onclick='openLightB('remove_ddi',500);' in to <a>open</a> with jquery function

my present code is like this

$(".remove_row").live("click", function(){
  $(".ddi tr:eq(2) td:eq(5) a").replaceWith("<a onclick='openLightB('remove_ddi',500);'>Remove</a>");
});

unfortunately result coming like this

<a remove_ddi',500);'="" onclick="openLightB(">Remove</a>

how to add onclick='openLightB('remove_ddi',500);' in to <a>open</a> with jquery function

my present code is like this

$(".remove_row").live("click", function(){
  $(".ddi tr:eq(2) td:eq(5) a").replaceWith("<a onclick='openLightB('remove_ddi',500);'>Remove</a>");
});

unfortunately result coming like this

<a remove_ddi',500);'="" onclick="openLightB(">Remove</a>
Share Improve this question asked Feb 28, 2012 at 16:16 Mo.Mo. 27.5k36 gold badges166 silver badges232 bronze badges 3
  • 3 the .live() function is deprecated and you should be using .delegate() or .on() depending on what version of jQuery you're using. – Surreal Dreams Commented Feb 28, 2012 at 16:19
  • and return openLight... with openlight returning false. Or use jQuery to assigh the click too – mplungjan Commented Feb 28, 2012 at 16:22
  • even delegate is deprecated... – dku.rajkumar Commented Feb 28, 2012 at 16:23
Add a comment  | 

6 Answers 6

Reset to default 9

How about letting jquery deal with escaping the quotes by using .attr():

$(".remove_row").live("click", function(){
  $(".ddi tr:eq(2) td:eq(5) a").attr('onclick', "openLightB('remove_ddi',500);");
});

DEMO


BTW, .live() is deprecated and could be removed from the library any time in the future. You should consider using .delegate() or .on() for event delegation.

You can fix your code by changing it as below,

$(".remove_row").live("click", function(){
  $(".ddi tr:eq(2) td:eq(5) a")
     .replaceWith("<a onclick=\'openLightB(\'remove_ddi\',500);\'>Remove</a>");
});

or simplify it,

 $(".remove_row").live("click", function(){
    $(".ddi tr:eq(2) td:eq(5) a").click (function () {
      $(this).text('Remove');
      openLightB('remove_ddi',500);
     });
 });

Also if you are using jQuery 1.7, then use .on

//replace <.remove_row container> with .remove_row container
 $('<.remove_row container>').on("click", '.remove_row', function(){
    $(".ddi tr:eq(2) td:eq(5) a").click (function () {
      $(this).text('Remove');
      openLightB('remove_ddi',500);
     });
 });

This work for quotes problems:

$(document).ready(function(){
    $(".remove_row").click( function(){
          $(".ddi tr:eq(2) td:eq(5) a").replaceWith("<a onclick=\"openLightB('remove_ddi',500);\">Remove</a>");
        });
});
$(".remove_row").live("click", function(){
  $(".ddi tr:eq(2) td:eq(5) a").attr('onclick', 'openLightB("remove_ddi",500);');
});

try this

<a href="javascript:openLightB('remove_ddi',500)">Remove</a>

jquery

$(".remove_row").live("click", function(){
  $(".ddi tr:eq(2) td:eq(5) a").prop('href','javascript:openLightB("remove_ddi",500);');
});
$(".remove_row").live("click", function(){
 $(".ddi tr:eq(2) td:eq(5) a").attr('onClick', 'openLightB("remove_ddi",500);'); });
发布评论

评论列表(0)

  1. 暂无评论