致电funtion on Button单击事件,而不是通过ID
大家好,我在两个带有#id的按钮上使用一个函数,问题是我必须两次编写相同的函数,所以我想用onclick调用它,以便我只能写一个函数。请帮助我是初学者
按钮
<a name="depIN" id="depIN" class="btn btn-primary" type="button">
<a name="depIN1" id="depIN1" class="btn btn-primary" type="button">
代码
$(document).ready(function(){
$('#depIN').click(function(){
let network = '';
if ($('#depsTo').val()){
network = $('#depsTo').val();
networkDesign = "";
for (i=0;i<network.length;i++){
temp = "<span style = 'border:3px dotted #399bff;'>" + network[i] + "</span>";
if (i !== network.length-1){
temp += "<bold style = 'color : #d77300;'> <i class='fa fa-long-arrow-right'></i></bold>"
}
network[i] = temp;
}
network = network.toString().replaceAll(',','');
$('#networkHeading').html(network);
}
});
});
另一个相同的功能,但按钮ID 不同
$(document).ready(function(){
$('#depIN1').click(function(){
let network = '';
if ($('#depsTo').val()){
network = $('#depsTo').val();
networkDesign = "";
for (i=0;i<network.length;i++){
temp = "<span style = 'border:3px dotted #399bff;'>" + network[i] + "</span>";
if (i !== network.length-1){
temp += "<bold style = 'color : #d77300;'> <i class='fa fa-long-arrow-right'></i></bold>"
}
network[i] = temp;
}
network = network.toString().replaceAll(',','');
$('#networkHeading').html(network);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样使用。
返回fals会阻止浏览器遵循链接
you can use it like this.
return false will prevent the browser from following the link
如果单击事件是相同的代码,则可以使用jQuery选择器
以
开始;$('[id^= sameIdstartwith]')
它将触发所有IDS从Depin,Depin2,Depin3等开始,ETC
DEMO:
If click events are the same code, you may use jQuery selecter
starts with
;$('[id^=sameIdStartWith]')
It will trigger all IDs starts with depIN, depIN2, depIN3, etc
Demo: https://jsfiddle.net/yeyene/n94au3gt/4/
有很多方法:
HTML代码:
一个是:
您可以在Selector jQuery中添加多个ID。
这是链接: fiddle
另一个是:
您可以将同一类给所有标签
jQuery代码:
这是另一个链接: fiddle
There were many ways:
HTML Code:
One is:
You can add multiple ids in selector jQuery.
Here is the link: Fiddle
another one is :
You can give same class to all tags
jQuery code:
Here is Another Link : Fiddle