第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Javascript 查詢選擇元素的選擇器

Javascript 查詢選擇元素的選擇器

慕妹3242003 2022-09-29 15:38:04
我目前正在嘗試以編程方式獲取,并以編程方式從純Javascript中的選擇元素中設(shè)置所選值的inerHTML。這是我的代碼:const select = document.querySelector("#myDiv select");  console.log(select.selectedOptions[0].option.innerHTML);如果我編寫控制臺.log(選擇.選中選項(xiàng)),我得到以下內(nèi)容:這是我想訪問和編輯的innerHTML內(nèi)部選項(xiàng),但我收到錯誤TypeError: Cannot read property 'option' of undefined我在網(wǎng)上沒有找到太多關(guān)于這個的信息,因此我在這里問。實(shí)現(xiàn)這一目標(biāo)的最佳方法是什么?謝謝!
查看完整描述

5 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個贊

select.selectedOptions[0]返回實(shí)際元素本身,因此您可以直接從中獲取和設(shè)置。optioninnerHTML


還要確保在選項(xiàng)上設(shè)置 了 一個,以便默認(rèn)情況下選擇某些內(nèi)容并且永遠(yuǎn)不會為空。selectedselect.selectedOptions


const select = document.querySelector("#myDiv select"); 

console.log("Initial:", select.selectedOptions[0].innerHTML);


const button = document.getElementById('toggle'),

      newText = document.getElementById('newText');

      

button.addEventListener('click', () => select.selectedOptions[0].innerHTML = newText.value);

<div id="myDiv">

  <select id="mySelect">

    <option value="option_a" selected>Option A</option>

    <option value="option_b">Option B</option>

  </select><br>

  <input id="newText" type="text" placeholder="Enter text here..."></input>

  <button id="toggle">Change</button>

</div>


查看完整回答
反對 回復(fù) 2022-09-29
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個贊

您可以使用css選擇器找到所選選項(xiàng)并閱讀其textContent


console.log(document.querySelector("#myDiv select option:checked").textContent);

<div id="myDiv">

  <select>

    <option value="1">one</option>

  </select>

</div>


查看完整回答
反對 回復(fù) 2022-09-29
?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個贊

只需刪除前面的內(nèi)部HTML。按照代碼操作。option


const select = document.querySelector("#myDiv select"); 

const optionSelected = select.selectedOptions[0]; 

console.log(optionSelected); 

optionSelected.innerHTML = 'New Demo'

強(qiáng)文本

工作演示


查看完整回答
反對 回復(fù) 2022-09-29
?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個贊

你是說這個?


選擇后不需要.選項(xiàng)[0]


const select = document.querySelector("#myDiv select"); 

console.log(select.selectedOptions[0].textContent); // or .text

// alternative

console.log(select.options[select.selectedIndex].textContent);

// setting the text:

select.selectedOptions[0].text = "Number one"

<div id="myDiv">

<select>

<option value="1">one</option>

</select>

</div>


查看完整回答
反對 回復(fù) 2022-09-29
?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個贊

在 JavaScript 中有一個單行代碼使用 :checked 偽類來獲取選定的文本或值。在這里,我們假設(shè)我們有一個 id=“搜索列表”的選擇。


const selected = document.querySelector('#searchList :checked');//returns selected node

console.log(selected.innerText);//get the selected option text

console.log(selected.value);//get the selected option value

驗(yàn)證這確實(shí)適用于選定元素中的選定選項(xiàng),以下是官方文檔:


:checked CSS 偽類選擇器表示選中或切換到 on 狀態(tài)的任何單選按鈕 ()、復(fù)選框 () 或選項(xiàng) ( 元素。


查看完整回答
反對 回復(fù) 2022-09-29
  • 5 回答
  • 0 關(guān)注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號