javascript Jquery如何在href中添加onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9485590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:49:54  来源:igfitidea点击:

Jquery how to add onclick in href

javascriptjquery-uijquery

提问by Mo.

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

如何添加onclick='openLightB('remove_ddi',500);'<a>open</a>使用jQuery功能

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>

回答by Didier Ghys

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

演示



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

顺便说一句,.live()弃用,将来可以随时从库中删除。您应该考虑使用.delegate().on()进行事件委托。

回答by Selvakumar Arumugam

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);
     });
 });

回答by Víctor Due?as Robles

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>");
        });
});

回答by James Skidmore

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

回答by dku.rajkumar

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);');
});

回答by ZakirK

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