如何使用querySelector为const赋予随机样式(top/left)?

huangapple go评论57阅读模式
英文:

How to give a random style (top/left) to the const with queryselector?

问题

我无法给starLoc常量使用JavaScript添加样式。
我遇到了这个错误:
script.js:14 Uncaught TypeError: Cannot read properties of undefined (reading 'style')
在starLoc[i].style.left = rndLeft + 'px'; //14行中

有问题的代码如下:

const btn = document.getElementById('btn');
const star = document.querySelector('.space');
const starLoc = document.querySelectorAll('.star-obj');
var rndLeft = Math.floor(Math.random()*1000);
var rndTop = Math.floor(Math.random()*1000);
function startSpacing(){
    btn.style.display = 'none';
    starMove();
}
function starMove(){
    for(var i = 0; i <= 60; i++){
        star.innerHTML += '<div class="star-obj">*</div>';
        starLoc[i].style.left = rndLeft + 'px'; //14行
        starLoc[i].style.top = rndTop + 'px'; 
    }
}

请注意,问题可能出在starLoc数组中。您需要确保starLoc中有足够的元素,以便通过索引i来访问。如果starLoc的长度小于i,将导致访问未定义的元素,从而引发JavaScript错误。

英文:

I cant give a style with js to the starLoc Const
i get this error:
script.js:14 Uncaught TypeError: Cannot read properties of undefined (reading 'style')
at starMove (script.js:14:20)

const btn = document.getElementById(&#39;btn&#39;);
const star = document.querySelector(&#39;.space&#39;);
const starLoc = document.querySelectorAll(&#39;.star-obj&#39;);
var rndLeft = Math.floor(Math.random()*1000);
var rndTop = Math.floor(Math.random()*1000);
function startSpacing(){
    btn.style.display = &#39;none&#39;;
    starMove();
}
function starMove(){
    for(var i = 0; i &lt;= 60; i++){
        star.innerHTML += &#39;&lt;div class=&quot;star-obj&quot;&gt;*&lt;/div&gt;&#39;;
        starLoc[i].style.left = rndLeft + &#39;px&#39;; //14 line
        starLoc[i].style.top = rndTop + &#39;px&#39;; 
    }
}
    &lt;body&gt;
        &lt;div class=&quot;start&quot;&gt;
            &lt;button id=&quot;btn&quot; onclick=&quot;startSpacing()&quot;&gt;Start Space Moving&lt;/button&gt;
        &lt;/div&gt;
        &lt;div class=&quot;space&quot;&gt;&lt;/div&gt;

        &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
    &lt;/body&gt;

what is wrong with const?

Do not judge strictly, I'm a beginner

答案1

得分: 1

你可以使用 document.getElementsByClassName 来获取一个元素的实时 HTMLCollection

const starLoc = document.getElementsByClassName('star-obj');

然而,在循环中,你应该真正使用 document.createElement

for (let i = 0; i <= 60; i++) {
    const div = document.createElement('div');
    div.textContent = '*';
    div.style.left = rndLeft + 'px';
    div.style.top = rndTop + 'px';
    star.append(div);
}
英文:

You can use document.getElementsByClassName to get a live HTMLCollection of elements.

const starLoc = document.getElementsByClassName(&#39;star-obj&#39;);

However, you should really use document.createElement in the loop instead.

for (let i = 0; i &lt;= 60; i++) {
	const div = document.createElement(&#39;div&#39;);
	div.textContent = &#39;*&#39;;
	div.style.left = rndLeft + &#39;px&#39;;
	div.style.top = rndTop + &#39;px&#39;;
	star.append(div);
}

答案2

得分: 1

在你的HTML中,你在调用startSpacing(),但实际函数名是starMove()。其次,你是动态生成元素,所以需要在每次调用函数时获取它们。这意味着需要将starLoc 移到函数内部。以下是带有color: red;的示例:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const star = document.querySelector('.space');
let rndLeft = Math.floor(Math.random() * 1000);
let rndTop = Math.floor(Math.random() * 1000);

function starMove() {
  for (let i = 0; i <= 60; i++) {
    star.innerHTML += '<div class="star-obj">*</div>';
    let starLoc = document.querySelectorAll('.star-obj');
    starLoc[i].style.color = 'red';
  }
}

<!-- language: lang-html -->
<div class="start">
  <button id="btn" onclick="starMove()">开始星空移动</button>
</div>
<div class="space"></div>

<!-- end snippet -->

以上是你要翻译的内容。

英文:

First, in your HTML you call startSpacing() while the function name is starMove(). Second, you dynamically generate elements, so you need to get them every time the function is called. This means moving starLoc to the function. See the example with color: red;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const star = document.querySelector(&#39;.space&#39;);
let rndLeft = Math.floor(Math.random() * 1000);
let rndTop = Math.floor(Math.random() * 1000);

function starMove() {
  for (let i = 0; i &lt;= 60; i++) {
    star.innerHTML += &#39;&lt;div class=&quot;star-obj&quot;&gt;*&lt;/div&gt;&#39;;
    let starLoc = document.querySelectorAll(&#39;.star-obj&#39;);
    starLoc[i].style.color = &#39;red&#39;;
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;start&quot;&gt;
  &lt;button id=&quot;btn&quot; onclick=&quot;starMove()&quot;&gt;Start Space Moving&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;space&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月9日 01:10:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定