次の操作の後に要素のIDを取得しようとしています:jQuery:テキストを含む要素のIDを取得する
私はリンクをクリックしてテキストを取得します。次に、そのテキストを比較して、そのテキストを含む要素を取得します。
新しく見つかった要素のIDを取得します。
しかし、最新の操作で私には未定義の結果が返されますので、あなたの助けを歓迎します。
$("#mybutton").click(function (e){
e.preventDefault();
var txt = $(e.target).text();
var findTxt = $(".list:contains('txt')");
console.log("findTxt returns : "+ findTxt.attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="mybutton">Sample Text</a>
<span id="idtofind" class="list">Sample Text</span>
<span id="another" class="list">another</span>
<span id="andanother" class="list">and another</span>
そうです。更新しました。 :) – uautkarsh