2012年04月17日
川俣晶の縁側ソフトウェア技術雑記 total 4743 count

jQuery mobileではボタンのラベルを書き換えられない?

Written By: 川俣 晶連絡先

 ははは。やっと理由が分かりましたよ。以下のコードは2つめのscript要素とlink要素をコメントアウトするとchangeを押したとき、ボタンのラベルが変わります。しかし、コメントアウトしないと変わりません。

サンプルソース §

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>

    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />

<script>

    function myfunction() {

        $("#Button1").val("ABC");

    }

</script>

</head>

<body>

    <input id="Button1" type="button" value="button" />

    <input id="Button2" type="button" value="change" onclick="myfunction();" />

</body>

</html>

理由 §

 実はjQuery mobileでボタンに見えるものは、ボタンをエミュレーションするHTML要素ツリーを自動的に生成しているだけ。なので、ボタンを書き換えても「ボタンに見えるもの」までは書き換わらないという仕掛けですね。

感想 §

 jQuery mobileは意外と便利そうに見えて制限に引っかかると大変。

追記 §

 対策としては、ボタンを使わないでdata-role="button"を指定したa要素を使えばいいのかな。

<a href="index.html" data-role="button">Link button</a>