亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用javascript獲取td的當前值?

如何使用javascript獲取td的當前值?

開滿天機 2023-10-17 17:36:20
我使用 thymeleaf 迭代 html 中的 td 值。我創建的網頁使得 td 的每個迭代元素都有 view,approve.disapprove 按鈕?,F在,當用戶按下 VIEW 按鈕時,我需要訪問與其對應的特定 td 值。關于如何在 javascript 中訪問 td 值的任何解決方案。?我目前在js中使用這個。但它返回整個列表值。不是特定的值。 var r = $("#testing #InputRuleName").text();    alert(r);網頁設計: <ol>            <table id="testing">             <tr th:each="RuleNameList : ${model.RuleNameList}">                                        <br/>                    <br/>              <li>  <td th:id="InputRuleName" th:text="${RuleNameList}" />                            <button type="button" onclick="sendresponse()" id="view">VIEW</button>                    <button type="button" onclick="sendresponse()" id="Approve">APPROVE</button>                    <button type="button" onclick="sendresponse()" id="DisApprove">DISAPPROVE</button> </li>                     <br/>                </tr>                </table>                </ol>
查看完整描述

3 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

event您可以從對象中訪問它

onclick="sendresponse(event)"
function sendresponse(e) {
 console.log(e.target.parentElement)
}


查看完整回答
反對 回復 2023-10-17
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

您可以捕獲tr級別的事件并寫入條件來識別源


更改 tr 并放置一個虛擬 CSS 類


<tr class="datarow"

現在為該 CSS 類選擇器編寫一個 jQuery


$(".datarow").on('click', function(e) {

     if(e.target.value == 'view') { //identify if this button is pressed

          //traverse above the button and traverse back inside

          alert( $("td", $(e.target).parent()).text() ); 

     }

});

說明$("td", $(e.target).parent()),jQuery允許在已選擇的元素內進行選擇,第一個參數是選擇器字符串,第二個參數是先前選擇的元素


查看完整回答
反對 回復 2023-10-17
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

如果您單擊 td,則可以使用currentTarget => https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

$('td').on('click',(el)=>{
    console.log(el.currentTarget)
}

如果沒有,您可以使用event.target => https://developer.mozilla.org/en-US/docs/Web/API/Event/target查看它的屬性

$('td').on('click',(el)=>{
    //On it's properties you can see parentNodes, parents, childs....
    console.log(el.target)
}


查看完整回答
反對 回復 2023-10-17
  • 3 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號