简体   繁体   English

Jquery如何在href中添加onclick

[英]Jquery how to add onclick in href

how to add onclick='openLightB('remove_ddi',500);' 如何添加onclick='openLightB('remove_ddi',500);' in to <a>open</a> with jquery function 使用jquery函数进入<a>open</a>

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 about letting jquery deal with escaping the quotes by using .attr() : 如何让jquery使用.attr()来处理转义引号:

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

DEMO DEMO


BTW, .live() is deprecated and could be removed from the library any time in the future. BTW, .live()弃用 ,可以在将来的任何时候从库中删除。 You should consider using .delegate() or .on() for event delegation. 您应该考虑使用.delegate().on()进行事件委派。

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 此外,如果您使用的是jQuery 1.7,请使用.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 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);'); });

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

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