致电funtion on Button单击事件,而不是通过ID

发布于 2025-02-11 22:04:11 字数 1640 浏览 3 评论 0 原文

大家好,我在两个带有#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);

    }
    });
});

Hello everyone i am using a function on two buttons with #ID, problem is that i have to write the same function two times, so i want to call it with Onclick, so that i can write a function only one time. please help i am a beginner

buttons

<a name="depIN" id="depIN" class="btn btn-primary" type="button">
<a name="depIN1" id="depIN1" class="btn btn-primary" type="button">

code

$(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);

    }
    });
});

another same function but different button 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┼── 2025-02-18 22:04:11

您可以这样使用。

<a id="myLink" title="Click to do something" href="PleaseEnableJavascript.html" onclick="MyFunction();return false;">link text </a>

返回fals会阻止浏览器遵循链接

you can use it like this.

<a id="myLink" title="Click to do something" href="PleaseEnableJavascript.html" onclick="MyFunction();return false;">link text </a>

return false will prevent the browser from following the link

梦萦几度 2025-02-18 22:04:11

如果单击事件是相同的代码,则可以使用jQuery选择器开始; $('[id^= sameIdstartwith]')
它将触发所有IDS从Depin,Depin2,Depin3等开始,ETC

DEMO:

$(document).ready(function(){
    $('[id^=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);
        }
    });
});

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/

$(document).ready(function(){
    $('[id^=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);
        }
    });
});
梦情居士 2025-02-18 22:04:11

有很多方法:

HTML代码:

<a id="b1" class="btnClick" type="button">B1M</a>
<a id="b2" class="btnClick" type="button">B2M</a>

一个是:
您可以在Selector jQuery中添加多个ID。

var b1 = $("#b1"), b2 = $("#b2");

b1.add(b2).click(function(){
   alert('clicked', this.id)
})

这是链接: fiddle

另一个是:
您可以将同一类给所有标签

jQuery代码:

$(".btnClick").click(function(){
   alert('clicked', this.id)
})

这是另一个链接: fiddle

There were many ways:

HTML Code:

<a id="b1" class="btnClick" type="button">B1M</a>
<a id="b2" class="btnClick" type="button">B2M</a>

One is:
You can add multiple ids in selector jQuery.

var b1 = $("#b1"), b2 = $("#b2");

b1.add(b2).click(function(){
   alert('clicked', this.id)
})

Here is the link: Fiddle

another one is :
You can give same class to all tags

jQuery code:

$(".btnClick").click(function(){
   alert('clicked', this.id)
})

Here is Another Link : Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文