【问题标题】:How can to use jquery regex selector?如何使用 jquery 正则表达式选择器?
【发布时间】:2013-09-19 10:30:34
【问题描述】:

请帮助我。 这不起作用,如何更正此代码??

<html>
<head>
    <script src="code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="code.jquery.com/jquery-migrate-1.1.0.min.js"></…; <script src="code.jquery.com/jquery-1.8.3.js"></script>
    <script src="code.jquery.com/ui/1.10.0/jquery-ui.js">        ; </script>
    <script>
        $(function () {
            $(document).on('click', 'div:regex(class,x)', function () {
                alert("sdfji");
            });
        });
    </script>
</head>
<body>
    <div class="x">
        XXXXXXXXXXXXXXXXXX</div>
</body>
</html>

【问题讨论】:

    标签: jquery selector


    【解决方案1】:

    由于您选择使用类,因此您可以使用正则表达式这样做

    <html>
    <head>
        <script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
        <script>
            $(function () {
                $(document).ready(function () {
                    $("div[class^=x]").click(function () {
                        alert($(this).attr('class'));
                    });
                });
            });
        </script>
    </head>
    <body>
        <div class="x">
            XXXXXXXXXXXXXXXXXX</div>
    </body>
    </html>
    

    这是一个有效的jsfiddle

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2019-04-23
      • 2012-06-12
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多