Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JQuery中区分多个id为的锚标记上的onclick-fun_Javascript_Jquery_Jquery Ui - Fatal编程技术网

Javascript 如何在JQuery中区分多个id为的锚标记上的onclick-fun

Javascript 如何在JQuery中区分多个id为的锚标记上的onclick-fun,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,你能建议我如何处理点击功能的多个锚定标签吗 我的代码如下: <a id="test1" href="#" >test1</a> <a id="test2" href="#" >test2</a> jQuery单击功能适用于所有锚标记,但我想根据id属性区分jQuery功能。您可以获得id属性 $('a').click(function(event) { alert($(this).attr('id')+' clicked!'); });

你能建议我如何处理点击功能的多个锚定标签吗

我的代码如下:

<a id="test1" href="#" >test1</a>
<a id="test2" href="#" >test2</a>

jQuery单击功能适用于所有锚标记,但我想根据id属性区分jQuery功能。

您可以获得id属性

$('a').click(function(event) {
    alert($(this).attr('id')+' clicked!');
});

您可以获得id属性

$('a').click(function(event) {
    alert($(this).attr('id')+' clicked!');
});

您可以读取元素的id,并根据id生成函数

$('a').click(function(event) {
    if ($(this).attr("id") == 'test1')
    {
        alert('Test 1 was clicked');
    }
    else if ($(this).attr("id") == 'test2')
    {
        alert('Test 2 was clicked');
    }
});

您可以读取元素的id,并根据id生成函数

$('a').click(function(event) {
    if ($(this).attr("id") == 'test1')
    {
        alert('Test 1 was clicked');
    }
    else if ($(this).attr("id") == 'test2')
    {
        alert('Test 2 was clicked');
    }
});

你想根据ID做些不同的事情吗

你可以这样做

$('a').click(function(e){
    var id = $(this).attr('id');
    // switch depending on id
});


但是,如果您要添加多个来执行相同的操作,这将不是很好。

您想根据ID执行不同的操作吗

你可以这样做

$('a').click(function(e){
    var id = $(this).attr('id');
    // switch depending on id
});


但是,如果要添加多个来执行相同的操作,这将不是一件很好的事情。

如果要基于id绑定到元素:

$('#test1').click(function(event) {
   alert('test1 clicked');
});

如果要基于id绑定到元素:

$('#test1').click(function(event) {
   alert('test1 clicked');
});

查看此页上的id,并做出if语句或建议使用开关:

$('a').click(function(event) {
    switch(this.id) {
        case 'test1':
            alert('link with an id of test1!');
            event.preventDefault();
            break;
        case 'test2':
            alert('link with an id of test2!');
            event.preventDefault();
            break;
        default:
            //Default behavior
    }
});

查看此页上的id,并做出if语句或建议使用开关:

$('a').click(function(event) {
    switch(this.id) {
        case 'test1':
            alert('link with an id of test1!');
            event.preventDefault();
            break;
        case 'test2':
            alert('link with an id of test2!');
            event.preventDefault();
            break;
        default:
            //Default behavior
    }
});
这是我的方法

$('a').click(
    function()
    {
        switch($(this).attr('id'))
        {
            case 'test1':
                // Do some work
                break;
            case 'test2':
                // Do some work
                break;
        }
    }
);
这是我的方法

$('a').click(
    function()
    {
        switch($(this).attr('id'))
        {
            case 'test1':
                // Do some work
                break;
            case 'test2':
                // Do some work
                break;
        }
    }
);

您可以查看其他答案中建议的id,但也可以使用自定义数据属性html5在每个标记上附加数据属性,或者使用href,稍后可以从onclick事件html5访问该属性

样本:

<a id="test1" data-my-custom-data="Insert" data-row-id="24" href="#" >test1</a>
<a id="test2" href="Insert" >test2</a>

$("a").click(function(event)
{
    alert($(this).attr("data-my-custom-data"));
    alert($(this).attr("data-row-id"));
    alert($(this).attr("href"));

    event.preventDefault(); // To avoid browsing to href...
});

您可以查看其他答案中建议的id,但也可以使用自定义数据属性html5在每个标记上附加数据属性,或者使用href,稍后可以从onclick事件html5访问该属性

样本:

<a id="test1" data-my-custom-data="Insert" data-row-id="24" href="#" >test1</a>
<a id="test2" href="Insert" >test2</a>

$("a").click(function(event)
{
    alert($(this).attr("data-my-custom-data"));
    alert($(this).attr("data-row-id"));
    alert($(this).attr("href"));

    event.preventDefault(); // To avoid browsing to href...
});