jQueryは要素を横断できません
-
27-10-2019 - |
質問
.singleanswerを選択しようとしていますが、機能していません。
jQuery:
$(".answerContainer").on("click", ".editAnswer", function(e){
e.preventDefault();
answer = $(this).parent('.answerBar').closest('.singleAnswer'); //this variable doenst work
HTML:
<div class='answerContainer'>
<p name='singleAnswer' class='singleAnswer'>$answer[$f]</p>
<input type='hidden' value='$answerid[$f]' class='answer_id' />
<p class='ratingBox'> $answerrating[$f]</p>
<div class='answerBar'>
<a href='#' class='upVote vote'>Upvote</a> · <a href='#' class='downVote vote'>Downvote</a> ·
<a class='answerTime'> $difference $periods[$j] ago</a>
· <a href='#' style='color: orange;' class='editAnswer'><b>Edit</b></a></div>
解決
.closest()
方法 検索 上 あなたが兄弟を探しているように見えるドムを通して .answerBar
要素、または、本当に、の子供 .answerContainer
エレメント。
これを試して:
answer = $(this).closest('.answerContainer').find('.singleAnswer');
これは、domを介して上がることを意味します this
最も近いへ .answerContainer
要素、そこからクラスの子孫を見つけます .singleAnswer
.
他のヒント
あなたは、現在の要素から検索を開始する最も近い兄弟を探しています。
answer = $(this).parent('.answerBar').siblings('.singleAnswer');
所属していません StackOverflow