回调jQuery元素

e0bqpujr  于 2022-12-03  发布在  jQuery
关注(0)|答案(3)|浏览(120)

我有按钮,我需要调用它,并采取(显示)身份证(号码)后,隐藏它。
我有这句台词:

<div id="follow_21" class="button rounded" title="">Follow</div>

我不知道如何调用(onclick)或其他方式这个按钮,并采取只有21和显示它在alert(');
我试过onclick="follow()

<div onclick="follow(21)" class="button rounded" title="">Follow</div>

和功能:

function follow(event){
    alert(event);
}

警报工作良好,但我不知道如何hide()元素通过点击它。假设你会帮助我。

oknwwptz

oknwwptz1#

使用jQuery's hide function

<div id="follow_21" class="button rounded" title="" onclick="$(this).hide();">Folow</div>

    or preferably something like:

    <div id="follow_21" class="button rounded" title="">Folow</div>
    <script type="text/javascript>
        //Binds a click event to all divs that start with "follow_"
        $('div[id^="follow_"]').click(function () {
            $(this).hide();
            var idSplit = $(this).attr('id').split('_')[1];
            alert('Follow - ' + idSplit);
        });
    </script>
khbbv19g

khbbv19g2#

你走对了路:

<div id="follow_21" onclick="follow(21)" class="button rounded" title="">Follow</div>

function follow(id){
    document.getElementById('follow_' + id).style.display = 'none';
}

或者更容易:

<div onclick="follow(this);" class="button rounded" title="">Follow</div>

function follow(div){
    div.style.display = 'none';
}

使用jquery和.click()

<div id="follow_21" class="button rounded" title="">Follow</div>

$('#follow_21').click(function() {
    $(this).hide();
}
idfiyjo8

idfiyjo83#

onclick属性中,this表示“此元素”。因此,可以将this传递给函数:
于飞:

<div id="follow_21" onclick="follow(this)" class="button rounded" title="">Follow</div>

JS:

function follow(el) {
    el.style.display = 'none';
}

这样,你就可以从HTML中去掉JS了。这被认为是一件非常好的事情。这在jQuery中非常容易,你可以完全去掉onclick属性:

$('#follow_21').click(function() {
    $(this).hide();
});

相关问题