簡體   English   中英

獲取特定元素內的元素

[英]Get an element inside a specific element

我有一個php代碼,它可以構建類似html的代碼:

echo '<button><span class="glyphicon glyphicon-zoom-in afficher_itk" aria-hidden="true"></span></button>';

echo' <div class="itk" id="'.str_replace(' ','',$systeme_culture['nom_plante']).'">
    <table class="table">
        <th><td>
            <tr colspan="2">
                   <label>Nom : </label>
                   <input class="nom_itk" id="nom_itk" value="test">
             </td</tr>
        </th>
     </table>
 </div>';

該代碼是動態生成的,我可以有多個這樣的代碼。

由於它可能具有類似的幾項內容,因此我想在運行JavaScript時定位right元素。

當我單擊按鈕時,我想在相應的input添加內容。 不是第一個,不是最后一個,而是僅在匹配的代碼中。

我在js中有類似的東西:

$(".afficher_itk").click(function(){
    $itk = $(this).closest(".itk"); //Here, I tried to select the closest itk class which correspond to the first div
    $itk.find(".nom_itk").val("test"); //Here I tried to search the input child
}

但是此代碼不起作用。 有人可以知道我該怎么做嗎?

不要在DOM樹中選擇最接近的itk ,最接近的遍歷。 而是使用.next從當前元素中選擇下一個itk

$(".afficher_itk").click(function(){
    $itk = $(this).next(".itk"); 
    $itk.find(".nom_itk").val("test");
});

我發現自己會怎樣。 我沒有選擇closest ,而是使用函數parents()獲得了td父對象。 之后,我可以使用find函數。

$itk = $(this).parents('td');
$itk.find('.nom_itk').val("lison");

嘗試這個:

如果元素是DOM對象,則可以使用

jQuery("html").on("click",".afficher_itk", function(){
    $itk = $(this).next(".itk"); 
    $itk.find(".nom_itk").val("test");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM