使用 jQuery Mobile 进行按钮操作,

发布于 2024-12-01 02:22:57 字数 1780 浏览 1 评论 0原文

使用 jQuery Mobile 动态构建按钮时遇到问题。复制并运行。请参阅代码中的注释。

<!DOCTYPE html>
<html>
  <head>
    <title>MSC Pre-Close Application</title>
    <meta charset="utf-8"/>

    <!-- standard Jquery/jQuery Mobile Libraries -->
    <script src="jquery.mobile/jquery.js"></script>
    <script src="jquery.mobile/jquery.mobile.js"></script>
    <link rel="stylesheet" href="jquery.mobile/jquery.mobile.css" />


<BR/>
<div data-role="page" id="mainmenu">
    <div data-role="header" data-position="inline">
    <h1>Main Menu</h1>
    </div>
    <div class="ui-body ui-body-c">
    <div data-role="content">   

        <div class="ui-block-a"><button id="click_me" type="submit" data-theme="a" data-icon="home">Click me to change the button to my right</button></div>
        <div class="ui-block-a"><button class="cl_pre_phone1" type="submit" rel="external" data-theme="a" data-icon="home">Primary Phone</button></div>

<script>
$(document).ready(function()
{
    // this changes the label but not the href action.
    $(".cl_pre_phone1").html("<a href='google.com'> Google</a>");

    $("#click_me").click(function() {
        alert("this should change the text and href of the button!");
        // here I would like to change the value as well as the href action, but it does not work.
        $(".cl_pre_phone1").html("<a href='yahoo.com'>Yahoo</a>");
        //$(".cl_pre_phone1").append("<a href='yahoo.com'>Yahoo</a>");
        //$(".cl_pre_phone1").append("<a href='yahoo.com'>Yahoo</a>").reload(true);

    });         
});

</script>

Having trouble constructing a button dynamically with jQuery Mobile. Copy and run. See notes in code.

<!DOCTYPE html>
<html>
  <head>
    <title>MSC Pre-Close Application</title>
    <meta charset="utf-8"/>

    <!-- standard Jquery/jQuery Mobile Libraries -->
    <script src="jquery.mobile/jquery.js"></script>
    <script src="jquery.mobile/jquery.mobile.js"></script>
    <link rel="stylesheet" href="jquery.mobile/jquery.mobile.css" />


<BR/>
<div data-role="page" id="mainmenu">
    <div data-role="header" data-position="inline">
    <h1>Main Menu</h1>
    </div>
    <div class="ui-body ui-body-c">
    <div data-role="content">   

        <div class="ui-block-a"><button id="click_me" type="submit" data-theme="a" data-icon="home">Click me to change the button to my right</button></div>
        <div class="ui-block-a"><button class="cl_pre_phone1" type="submit" rel="external" data-theme="a" data-icon="home">Primary Phone</button></div>

<script>
$(document).ready(function()
{
    // this changes the label but not the href action.
    $(".cl_pre_phone1").html("<a href='google.com'> Google</a>");

    $("#click_me").click(function() {
        alert("this should change the text and href of the button!");
        // here I would like to change the value as well as the href action, but it does not work.
        $(".cl_pre_phone1").html("<a href='yahoo.com'>Yahoo</a>");
        //$(".cl_pre_phone1").append("<a href='yahoo.com'>Yahoo</a>");
        //$(".cl_pre_phone1").append("<a href='yahoo.com'>Yahoo</a>").reload(true);

    });         
});

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-12-08 02:22:57

jQM 所做的增强使您陷入困境。您的按钮将被重写为:

<span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Primary Phone</span>
<span class="ui-icon ui-icon-home ui-icon-shadow"></span></span>
<button class="cl_pre_phone1 ui-btn-hidden" type="submit" data-theme="a" data-icon="home"  aria-disabled="false">Primary Phone</button>

解决此问题的一种方法是将回调内的调用更改为:

$(".cl_pre_phone1").attr("href", "http://yahoo.com").parent().find(".ui-btn-text").text("Yahoo");

或者您可以替换整个 ui-block-a div。

You're getting messed up by the enhancement which jQM does. Your button is getting rewritten to:

<span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Primary Phone</span>
<span class="ui-icon ui-icon-home ui-icon-shadow"></span></span>
<button class="cl_pre_phone1 ui-btn-hidden" type="submit" data-theme="a" data-icon="home"  aria-disabled="false">Primary Phone</button>

One way round this would be to change the call inside the callback to:

$(".cl_pre_phone1").attr("href", "http://yahoo.com").parent().find(".ui-btn-text").text("Yahoo");

or you could replace the whole ui-block-a div.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文