如何通过在PC上单击或在Android上通过触摸来更改句子中单词的颜色

阿波斯托洛斯

我正在尝试为孩子们制作一个游戏,在该游戏中,我将通过在PC上单击某个单词或在Android中触摸该单词来隐藏一个单词,然后通过执行该操作来揭示答案。我考虑过将句子拆分为数组元素,然后实现更改该句子颜色的功能。我对这种方法正确吗?预先谢谢大家。

      <p id="hExample">Hello my world </p>

       <script>
       //Split sentence into array elements
           var jExample = document.getElementById("hExample").innerHTML;
           var jElements = jExample.split(" ");

       //Return array elements into the sentence position
           for (var y=0; y< jElements.length; y++) {
               document.getElementById("hExample").innerHTML += '<span id=' + y + '>' + jElements[y] + " " + "</span> " ;
           }

           document.getElementById('hExample').onclick = changeColor;   

           function changeColor() {
               if ( document.getElementById("hExample").style.color = "white") {
                   document.getElementById("hExample").style.color = "red";
               }else{
                   document.getElementById("hExample").style.color = "white";
               }       
           }   

       </script>
特里摩斯

您走在正确的轨道上,但是有一个错误:

document.getElementById('hExample').onclick = changeColor;

这会在“ hExample”段落上设置一个click事件观察器,但是您要检测“ hExample”中各个跨度上的点击。这可以通过检查event.target属性来完成,该属性将等于单击的跨度。确定单击哪个跨度的代码如下所示:

document.getElementById('hExample').addEventListener('click',
  function (event) {
    let spanClicked = event.target;
    // do something with the span
});

这是您的代码,并进行了一些修订。

如果不清楚任何代码,请询问。

const hExample = document.getElementById('hExample');

// Split sentence into array of words
var jExample = hExample.innerHTML;
var jElements = jExample.split(" ");

// wrap each word in a span
let content = "";
for (var y = 0; y < jElements.length; y++) {
  content += '<span id=' + y + '>' + jElements[y] + " " + "</span> ";
}

// add all spans to hExample
hExample.innerHTML = content;

// listen for clicks inside hExample
hExample.addEventListener('click', function(evt) {

  // get the clicked word
  const word = evt.target;
  
  // add or remove the hidden class
  word.classList.toggle('hidden-span');
  
  // make the word red
  word.style.color = "red";
});
/* hide the span */
.hidden-span {
  opacity: 0;
}
<p id="hExample">Hello my world </p>

修订—单击时用下划线替换单词

下方有一个新注释,要求单击后的单词用下划线替换。

使用不同的方法可以轻松解决该问题。

请参见下面的代码:

  const hExample = document.getElementById('hExample');

  // Split sentence into array of words
  let jExample = hExample.innerHTML;
  let words = jExample.split(" ");

  // wrap each word in a span
  let content = "";
  for (const word of words) {
    content += `<span>${word}</span> `;
  }

  // add all spans to hExample
  hExample.innerHTML = content;

  // handle click on a word
  function handleWordClick (evt) {
    // get the span that was clicked
    const span = evt.target;

    if (span.dataset.word) {
      // extract word from span's 'data-word' attribute and display in red
      span.innerHTML = span.dataset.word;
      delete span.dataset.word;
      span.style.color = 'red';
    } else {
      // maintain current width of span
      span.style.width = span.getBoundingClientRect().width + "px";
      // store word in span's `data-word' attribute
      span.dataset.word = span.innerHTML;
      // show blue "_" in place of word
      span.innerHTML =  "_";
      span.style.color = 'blue';
    }
  }

  // listen for clicks inside hExample
  hExample.addEventListener('click', handleWordClick);
span {
  /* this will preserve the span's width */
  display: inline-block;
  /* this will center text inside span */
  text-align: center;
}
<p id="hExample">Hello my world </p>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过在移动设备上触摸来更改对象属性 (UNITY) (2D)

如何在Android Studio中通过触摸来移动,旋转和调整文本大小?

如何通过第一次单击更改 CardView 的边框颜色,然后在第二次单击 Android 上再次更改回以前的颜色?

如何通过单击Javascript中的按钮来更改笔触的颜色?

如何通过单击更改元素颜色

如何通过单击更改svg元素的颜色?

如何通过单击按钮更改文本颜色?

如何通过十六进制更改Java上的布局颜色?

如何通过Android上的OpenGL ES 2.0的片段着色器更改颜色

如何通过Google Chrome在PC上运行Android应用?

如何在Android和iOS上通过Javascript检测设备上的触摸?

如何决定通过邮寄或在Rails上运行哪个动作

如何通过在UISearchbar中包含单词来过滤句子

如何通过Java代码在android中更改颜色值

如何通过kotlin代码在android中更改笔触颜色?

如何通过ADB在Android中显示触摸?

Android如何通过文本更改按钮颜色

如何通过单击var短语数组句子上的按钮来添加CSS或JavaScript可重复打字机效果?

通过单击更改图像颜色的Android Studio

如何通过单击图标来更改超棒字体的颜色

如何通过单击图表栏来更改颜色?

如何通过单击按钮更改项目的颜色?

如何通过单击按钮更改SVG元素的颜色?

如何通过单击连续更改跨度的颜色和文本?

如何通过单击更改多段线颜色?

通过在处理中单击鼠标来更改对象颜色

通过在javascript中单击btn更改主体背景颜色

如何通过单击 React 上的按钮在屏幕上显示文本字段中的文本

如何通过JavaScript更改背景颜色来覆盖对象上的所有CSS选择器?