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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

用黃色背景突出顯示所有長度超過 8 個(gè)字符的單詞。如何在不使用 jQuery 的情況下僅使用

用黃色背景突出顯示所有長度超過 8 個(gè)字符的單詞。如何在不使用 jQuery 的情況下僅使用

蝴蝶刀刀 2022-12-02 16:55:34
我已經(jīng)開始通過實(shí)踐練習(xí)學(xué)習(xí) JavaScript。我嘗試通過以下方式解決這個(gè)問題,但它不起作用!任何線索將不勝感激。window.onload = function() {  check = (word) => {    if (word.length > 8) {      word.style.color = "blue";    } else {      word;    }  }  func = () => {    var str = document.querySelector("#Second").innerText;    var newt = str.trim().split(' ').map(check).join(' ');    document.querySelector("#Second").innerText = newt;  }}
查看完整描述

3 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

我認(rèn)為你的問題在于check()功能。你已經(jīng)正確地分析了問題,但是你不了解 DOM 所以你做錯(cuò)了。


首先,你檢查的單詞是純字符串(它是一個(gè)字符數(shù)組,所以你可以用length屬性檢查它)。其次,它.style.color只是 Element DOM 對(duì)象的子對(duì)象。字符串不能那樣做。


由于這個(gè)問題,您必須將剛剛檢查的字符串轉(zhuǎn)換為 Element DOM 對(duì)象。根據(jù)情況,有很多方法可以做到這一點(diǎn)。我假設(shè)輸出將是這樣的:


document.body.innerHTML += word

如果是這樣的話,你就可以放心了。只需將 包裝word在這個(gè) html 代碼字符串中。剩下的你已經(jīng)很好地解決了。


(我知道你用的是innerText,但我覺得innerHTML更簡單,所以我選擇了它。如果你真的需要使用innerText,請(qǐng)?jiān)谙旅嬖u(píng)論,我會(huì)更新帖子)


這是我的代碼:


window.onload = function() {

  const check = word => {

    if (word.length > 8) {

      return '<span class="hightlight">' + word + '</span>'

    } else {

      return word

    }

  }


  const sample = document.querySelector("#sample")

  sample.innerHTML = sample

    .innerText

    .trim()

    .split(' ')

    .map(check)

    .join(' ')

}

#sample {}


.hightlight {

  background: yellow

}

<p id='sample'>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


我的建議。在做任何事情之前,試著理解你正在處理的變量的類型。


查看完整回答
反對(duì) 回復(fù) 2022-12-02
?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

window.onload = function() {

  check = (word) => {

    if (word.length > 8) {

      word = '<span style="background:yellow;">' + word + '</span>';

    } else {

      word;

    }

    return word;

  }


  var str = document.querySelector("#Second").innerText;

  var newt = str.trim().split(' ').map(check).join(' ');

  document.querySelector("#Second").innerHTML = newt;


}

<div id="Second">Short short thiswordislong short short thiswordislongtoo short</div>


查看完整回答
反對(duì) 回復(fù) 2022-12-02
?
墨色風(fēng)雨

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊

帶有 .|,|? 的版本 檢測(cè)


const setup = () => {

  const p = document.querySelector('p');


  wrapWordsWithLengthEight(p);

}


const check = (word) => {


  //Check if word include a .|,|?

  const hasCharacter = word.includes(".", word.length - 1)

  || word.includes(",", word.length - 1)

  || word.includes('?', word.length - 1);

  //Calculate real word length

  const wordLength = hasCharacter ? (word.length -1) : word.length;

  if(wordLength > 8) {

    const spanContent = hasCharacter ? word.substring(0, word.length - 1) : word;

    const endCharacter = hasCharacter ? (word.substring(word.length - 1)) : '';

    word = `<mark>${spanContent}</mark>${endCharacter} `;

  }

  else word = `${word} `;


  return word;

};


const wrapWordsWithLengthEight = (target) => {

  //Render HTML to target

  const text = (target.textContent).trim().split(' ').map(check).join('');

  target.innerHTML = text;

}


window.addEventListener('load', setup);

<p>

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique enim quod quos, modi architecto, praesentium tenetur suscipit assumenda, sit neque odit, illum minima voluptates? Dolor non dolore accusamus quam maiores.

</p>


查看完整回答
反對(duì) 回復(fù) 2022-12-02
  • 3 回答
  • 0 關(guān)注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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