1

私は動的に生成されたリストビューを持っています。各リスト要素には、カスタム データ属性を割り当てます。listitem がクリックされると、ポップアップが呼び出されます。

            <a  href="#popupMenu" data-name='.$myrow[id].'  data-rel="popup">View</a>

//mypopup

            <ul data-role="listview" data-inset="true"  data-theme="b">
                <li data-role="divider" data-theme="a">Options</li>
                <li><a id="one" onclick="func1();" href="#">Function one</a></li>
                                    <li><a id="two" href="#">Remove</a></li>
                                    <li><a id="three" href="#">Cancel</a></li>
            </ul>

ポップアップを呼び出したリストビュー項目のカスタム データ属性を取得する正しい方法を知りたいです。

//myjavascipt

  var func1 = function() {
        //display the custom data attribute from the listview click here
                 alert();
     };

ありがとう。

4

3 に答える 3

0

クリックリスナーを介してイベントを渡す必要があります

<li><a id="one" onclick="func1(event);" href="#">Function one</a></li>

イベントを使用して、必要な情報を取得します

function func1(e){
 var role = e.target.parentNode.parentNode.getAttribute("data-role");
 document.querySelector('#popupMenu').innerHtml = role;
}

これが実際の例です: http://codepen.io/positlabs/pen/DAtCL

于 2013-10-07T01:39:46.717 に答える
0

質問に JQuery タグを含めたという理由だけで、JQuery ベースの回答を投稿しています。そのため、プロジェクトに既に JQuery があると想定しています。そう:

.attr()メソッドを使用するだけです。

その「データ」属性がアンカータグ自体にある場合、それは非常に簡単なはずです(すべてのアンカータグが「func1()」を指しているonclickを持っていると仮定します:

    //display the custom data attribute from the listview click here
    alert($(this).attr('data-role'));
    alert($(this).attr('data-name'));
    alert($(this).attr('data-rel'));
于 2013-10-07T01:53:43.950 に答える
0

これはあなたのために働くかもしれません:

<a id="one" onclick="func1(this)" href="#">Function one</a>
var func1 = function (target) {
    alert($('a').filter(function () {
        return $(this).attr('href') === '#' + $(target).closest('ul').parent().attr('id');
    }).data('name'));
}
于 2013-10-07T04:18:37.410 に答える