JavaScript slice / substring仅在替换HTML innerText时返回未定义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript slice / substring仅在替换HTML innerText时返回未定义相关的知识,希望对你有一定的参考价值。

因此,我编写了一个简单的RGB滑块,可在html画布上显示其结果。除了slice / substring之外,所有其他功能都有效:由于某些原因,将其设置为变量或更改<span>的innerText时,它将返回undefined

      const $ = ele => document.querySelector(ele),
      numToHex = num => parseInt(num, 10).toString('16').padStart(2, '0'),
      c = $('#canvas').getContext('2d')
      
      var rsp = $('#rspan'),
      gsp = $('#gspan'),
      bsp = $('#bspan'),
      rvalue, gvalue, bvalue
      
      $('#rrange').oninput = () => {
        rvalue = `#${numToHex($('#rrange').value).toUpperCase()}0000`
        rsp.innerText = rvalue
        rsp.style.color = rvalue
        updateCanvas()
      }
      
      $('#grange').oninput = () => {
        gvalue = `#00${numToHex($('#grange').value).toUpperCase()}00`
        gsp.innerText = gvalue
        gsp.style.color = gvalue
        updateCanvas()
      }
      
      $('#brange').oninput = () => {
        bvalue = `#0000${numToHex($('#brange').value).toUpperCase()}`
        bsp.innerText = bvalue
        bsp.style.color = bvalue
        updateCanvas()
      }
      
      function updateCanvas(rval = rvalue, gval = gvalue, bval = bvalue) {
        console.log(rval.substring(0, 3)) // This works
        let arr = [rval.substring(0, 3), gval.substring(3, 5), bval.substring(-2)]
        c.fillStyle = arr.join('') // Throws error
        c.fillRect(0, 0, 250, 250)
      }
      input {
        width: 300px;
      }
<!DOCTYPE html>
<html>
  <head>
    <title>RGB Slider</title>
  </head>
  <body>
    <canvas id="canvas" width="250" height="250"></canvas>
    <div id="rdiv">
      <input type="range" min="0" max="255" id="rrange" />
      <span id="rspan"></span>
    </div>
    <div id="gdiv">
      <input type="range" min="0" max="255" id="grange" />
      <span id="gspan"></span>
    </div>
    <div id="bdiv">
      <input type="range" min="0" max="255" id="brange" />
      <span id="bspan"></span>
    </div>
  </body>
</html>

为什么会这样?它也与slice引发相同的错误。为何它在先前的console.log语句中起作用?

答案

您目前并不总是定义rvaluegvaluebvalue-它们以undefined开头,并且只有在每个滑块至少被更改一次之后,这3个变量才被分配给所有。在此之前,updateCanvas

function updateCanvas(rval = rvalue, gval = gvalue, bval = bvalue) {

将至少具有一个未定义的参数,因此将尝试访问这些未定义的参数上的.substring

将颜色值默认设置为范围的中间,#800000

rvalue = '#800000',
gvalue = '#800000',
bvalue = '#800000';

[构造数组时,您还需要适当地对子字符串进行切片-从红色,绿色和蓝色中分别取2个字符,然后将它们放在一起并以#作为前缀:

const $ = ele => document.querySelector(ele),
      numToHex = num => parseInt(num, 10).toString('16').padStart(2, '0'),
      c = $('#canvas').getContext('2d')
      
      var rsp = $('#rspan'),
        gsp = $('#gspan'),
        bsp = $('#bspan'),
        rvalue = '#800000',
        gvalue = '#800000',
        bvalue = '#800000';
      
      $('#rrange').oninput = () => {
        rvalue = `#${numToHex($('#rrange').value).toUpperCase()}0000`
        rsp.innerText = rvalue
        rsp.style.color = rvalue
        updateCanvas()
      }
      
      $('#grange').oninput = () => {
        gvalue = `#00${numToHex($('#grange').value).toUpperCase()}00`
        gsp.innerText = gvalue
        gsp.style.color = gvalue
        updateCanvas()
      }
      
      $('#brange').oninput = () => {
        bvalue = `#0000${numToHex($('#brange').value).toUpperCase()}`
        bsp.innerText = bvalue
        bsp.style.color = bvalue
        updateCanvas()
      }
      
      function updateCanvas(rval = rvalue, gval = gvalue, bval = bvalue) {
        let arr = [rval.substring(1, 3), gval.substring(3, 5), bval.substring(5, 7)]
        console.log(arr);
        c.fillStyle = '#' + arr.join('') // Throws error
        c.fillRect(0, 0, 250, 250)
      }
input {
        width: 300px;
      }
<!DOCTYPE html>
<html>
  <head>
    <title>RGB Slider</title>
  </head>
  <body>
    <canvas id="canvas" width="250" height="250"></canvas>
    <div id="rdiv">
      <input type="range" min="0" max="255" id="rrange" />
      <span id="rspan"></span>
    </div>
    <div id="gdiv">
      <input type="range" min="0" max="255" id="grange" />
      <span id="gspan"></span>
    </div>
    <div id="bdiv">
      <input type="range" min="0" max="255" id="brange" />
      <span id="bspan"></span>
    </div>
  </body>
</html>

以上是关于JavaScript slice / substring仅在替换HTML innerText时返回未定义的主要内容,如果未能解决你的问题,请参考以下文章

javascript中slice(),splice(),split(),substring(),substr()使用方法

javascript中substring()substr()slice()的区别

JavaScript slice / substring仅在替换HTML innerText时返回未定义

substring() , slice() and substr()方法

区分substr、substring、slice、split、splice

javascript中常用操作字符串的几种方法charAt()indexOf()slice()substr()