jquery + html get

gor*_*n33 -3 html jquery class

我想知道如何制作这样的东西:

例:

<a href="#" id="123">Link</a>

当我点击它时,我想得到id=123jQuery或类似的东西.

然后将其添加id=123到另一个div:

像这样

<div class='info'>id=123</div>

你能帮助我吗?

Dar*_*rov 5

您可以订阅.click()所有锚点的事件(或通过使用CSS类选择器缩小它们),并在此事件中获取被单击的锚点的id并使用它来设置目标div的html:

$(function() {
    $('a').click(function() {
        // this will contain the id of the anchor that was clicked
        var id = this.id;
        $('.info').html('id=' + id);
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 和:.html('id ='+ id); 对于实际字符串"id = 123". (2认同)