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
Jquery how to add onclick in href
提问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);");
});
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);'); });