0

我正在尝试在使用 jQuery Mobile 的移动应用程序中使用未链接的按钮。我仍然希望按钮是可点击的,但我想使用我自己的而不是默认的 ajax 行为。

但是,当我点击一个未链接到某项内容的按钮时,它不会提供任何反馈。也就是说CSS不会更改为按下状态然后再返回。当我点击具有有效 href 或位于表单内的链接时,它可以工作。

奇怪的是,在我的桌面浏览器中单击链接确实会调用更改,尽管它与点击按钮不同。使用默认的 CSS 包按钮,当点击它们提交表单或进入下一页时,它们会立即变为蓝色。当我在桌面上单击它们时,它们会进入灰色悬停状态,因此看起来这实际上不适用于任何浏览器。

有谁知道如何获得一个简单的按钮来响应点击,即使该按钮实际上没有链接到任何地方?

编辑:这里有一些代码可以尝试我在 iOS 和 Android 上都尝试过,结果相同。

<!DOCTYPE html>
<html>
    <head> 
        <title>My Page</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.js"></script>
    </head>

    <body>

        <div data-role="page" id="startpage">
            <div data-role="header" data-theme="c">
                <h1>Button Test</h1>
            </div>
            <div data-role="content">
                <button id="test1">Test 1</button>
                <button data-role="button" id="test2">Test 2</button>
                <a data-role="button" id="test3">Test 3</a>
                <a data-role="button" href="" id="test4">No href</a>
                <a data-role="button" href="#" id="test5">href=#</a>
                <a data-role="button" href="#page2" id="test6">This works because it's linked</a>
                <form method="get" action="index2.html">
                    <input type="text" name="name" placeholder="Name"/>
                    <button type="submit" value="No tap change here either"></button>
                </form>
            </div><!-- /content -->
        </div><!-- /page -->

        <div data-role="page" id="page2">
            <div data-role="header" data-theme="c">
                <h1>Page 2</h1>
            </div>
            <div data-role="content">Linked buttons work</div>
        </div>

    </body>
</html>
4

1 回答 1

1

好的,我最终在 jQuery Mobile 问题跟踪器中提交了一个错误,这似乎是一个已知问题。

https://github.com/jquery/jquery-mobile/issues/5009 https://github.com/jquery/jquery-mobile/issues/4469

解决方法是使用这样的链接按钮:

<a href="javascript:void(0)" data-role="button">Click me</a>

有了这个按钮,即使它实际上并没有去任何地方或做任何事情,它也会向用户提供反馈。

他们正在考虑在 v1.3 中解决这个问题。

于 2012-09-16T01:52:08.183 回答