Perform a click event on the li tag under ul

Source of the problem: When working on a project, general data is dynamically loaded in a loop. The structure is the same, but the bound values ​​are different. How to deal with the same label.

Click whoever to show whose value:


<ul id="test">   

  <li class="test" >1</li>

<li class="test" >2</li>

<li class="test" >3</li>

<li class="test" >4</li>

</ul>


1. If there is no class attribute

Outside the same label (just find one, you need to wrap all the same labels)

(Ul wraps all li, the first parameter in on: the type of event that needs to be done, the second parameter: who is the object that executes this event)


$("ul#test").on("click","li",function(){

alert($(this).text());

})


  2、

<ul id="test">   

 <li class="test" >
        <div class="div-text" style="width: 100%; height: 100%;">
            <p class="projectName" >${ProjectName}</p>
            <p class="stationName" >${StationName}</p>
            <p class="time">${UpdateTime}</p>
            <p class="ip" >${TesterIP}</p>
        </div>
    </li>

</ul>

$("ul#test").on("click","li",function(){

 alert($(this).find("p.ip").text());

})




Guess you like

Origin blog.csdn.net/qq_27740983/article/details/76154317