码迷,mamicode.com
首页 > Web开发 > 详细

前端: jquery事件绑定及选择器使用动态变量

时间:2018-03-01 17:14:04      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:jquery   selector   

目标与html代码

目标:点击jq删除,获取整行tr的id,(后端部分略,传递id处理即可),页面删除该tr行

  <tr myid="{{ cls.id }}">
     ....

      <td><a onclick="rmStu(this);" class="abc" href="#">jq删除</a></td>

【方式1】由onclick触发,点击位置由this传递

动态变量由字符串拼接

<script>
    function rmStu(th) {
    var myid=$(th).parent().parent().attr(‘myid‘);
    alert(myid);
{#    $(th).parent().parent().remove();#}
{#    $("[myid=‘11‘]").remove();#}
{#    $("[myid=myid]").remove();无法定位#}
    $("[myid="+myid+"]").remove();

    }
</script>

【方式2】事件绑定。点击触发,条件为任意class="abc"时。

触发时再由$(this)获取点击位置

<script>
    $(function () {
        bindEvent();
    });
    function bindEvent() {
        $(‘.abc‘).click(function () {
            var myid=$(this).parent().parent().attr(‘myid‘);
            alert(myid);

        })
    }

</script>

附录 bootstrap modal show

<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/plugin/bootstrap-3.3.7/js/bootstrap.js"></script>
<script>
    $(function () {
        abc();
    });
    function abc() {
        $(‘#showmyModal‘).click(function () {
            $(‘#myModal‘).modal(‘show‘);
        })
    }

</script>
【1】 点击即传递位置
【2】 点击触发函数,多处都可触发,之后获取位置
【3】 howto 动态id,绑定方式,点击即传递?

前端: jquery事件绑定及选择器使用动态变量

标签:jquery   selector   

原文地址:http://blog.51cto.com/13606158/2074340

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!