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

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

如何在最近的標簽中找到類?

如何在最近的標簽中找到類?

躍然一笑 2023-05-25 16:51:00
如何使用 Java Script 在最接近的標簽中找到類?需要在 . 例子如下:       <tr>            <td class="customer">                @Html.DisplayFor(modelItem => item.Cust)            </td>            <td>                <select id="decisionList">                    <option value="0" selected></option>                    <option value="1">None</option>                </select>            </td>       </tr>我在 JS 中試過這個:document.querySelectorAll("#decisionList").forEach(elem => elem.addEventListener("change", function () {    var selectedOptionIndex = this.options[this.selectedIndex].index;  //this works perfectly    var invoiceDateText = this.closest('tr').find('customer').textContent; //this doesn't work// ...some other code
查看完整描述

2 回答

?
幕布斯7119047

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

我想這就是你要找的:


document.querySelector("#decisionList").addEventListener('change', function() {

  const selectedOptionIndex = this.options[this.selectedIndex].index;

  const invoiceDateText = this.closest('tr').querySelector('.customer').textContent;

});


查看完整回答
反對 回復 2023-05-25
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

closest方法使用querySelector來查找最近的。

    var invoiceDateText = this.closest('.customer').textContent;

然后,這應該在文檔根目錄中搜索DOM與客戶類最接近的元素。

如果您想搜索最近的tr帶有customer類的單元格,而不是將其修改為。

    var invoiceDateText = this.closest('tr>.customer').textContent;


查看完整回答
反對 回復 2023-05-25
  • 2 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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