如何创建一个在按钮点击时动态分配变量的循环。

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

How to create a loop that assigns variables dynamically when button clicked

问题

我正在尝试根据点击的按钮来分配变量。
首先,我这样做:

$("#dot"+1).click(function(){
        logdot1=true;
});
$("#dot"+2).click(function(){
        logdot2=true;
});
$("#dot"+3).click(function(){
        logdot3=true;
});
// ...一直重复直到36次

我想通过循环来减少这段代码。该怎么做?

我尝试这样做:

var logdot;
$("#dot"+i).click(function(){
        this['logdot'+i]=true;
});

但这会导致整个脚本停止工作。

英文:

I am trying to assign variables based on buttons clicked.
First I did like this:

$("#dot"+1).click(function(){
        logdot1=true;
});
$("#dot"+2).click(function(){
        logdot2=true;
});
$("#dot"+3).click(function(){
        logdot3=true;
});
....and so on up to 36 times

I want to minimize this code through loop.How do I do that?

I tried doing it like this:

var logdot;
$("#dot"+i).click(function(){
        this['logdot'+i]=true;
});

But this stops the whole script from working.

答案1

得分: 1

你的 this 可能不是你所想的。

给每个点添加一个类名 dotClass

有一个常量 dotsClicked = [];

并使用委托(假设这些点是容器中的兄弟节点)

在 jQuery 和纯 JS 中都不需要循环来实现这个功能。

你可以在纯 JS 中将委托交给一个公共容器

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-js -->
// 测试数据
$('#dots').html(Array.from({ length: 36 })
  .map((_, i) => '<div class="dot"></div>').join('') // 纯 JS map
);

let dotsClicked = $('.dot').map(function() {
  return this.matches('.dotClicked')
}).get(); // 使用纯 JS matches 的 jQuery map

$('.dot').on('click', function() {
  $(this).toggleClass('dotClicked');
  dotsClicked[$(this).index()] = $(this).is('.dotClicked');
  console.log(dotsClicked);
})

<!-- 语言: lang-css -->
#dots {
  display: flex;
  gap: 10px;
  flex-direction: row;
  flex-wrap: wrap;
  width: 130px;
  border: 1px solid black;
  padding: 10px;
}

.dot {
  font-size: 35px;
}

.dot:hover {
  color: red;
}

.dot::before {
  content: "•";
}

.dotClicked {
  color: red;
}

<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<div id="dots">
</div>

<!-- 结束代码片段 -->
英文:

Your this is likely not what you think it is.

Give each dot a class of dotClass.

Have a const dotsClicked = [];

and use delegation (assuming the dots are siblings in the same container)

There is no need for loops in neither jQuery nor vanilla JS to do this.

You can delegate to a common container in vanilla JS

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

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

// test data
$(&#39;#dots&#39;).html(Array.from({ length: 36 })
  .map((_, i) =&gt; `&lt;div class=&quot;dot&quot;&gt;&lt;/div&gt;`).join(&#39;&#39;) // vanilla JS map
);

let dotsClicked = $(&#39;.dot&#39;).map(function() {
  return this.matches(&#39;.dotClicked&#39;)
}).get(); // jQuery map using vanilla JS matches

$(&#39;.dot&#39;).on(&#39;click&#39;, function() {
  $(this).toggleClass(&#39;dotClicked&#39;);
  dotsClicked[$(this).index()] = $(this).is(&#39;.dotClicked&#39;);
  console.log(dotsClicked);
})

<!-- language: lang-css -->

#dots {
  display: flex;
  gap: 10px;
  flex-direction: row;
  flex-wrap: wrap;
  width: 130px;
  border: 1px solid black;
  padding: 10px;
}

.dot {
  font-size: 35px;
}

.dot:hover {
  color: red;
}

.dot::before {
  content: &quot;•&quot;
}

.dotClicked {
  color: red;
}

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;dots&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月16日 14:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487365.html
匿名

发表评论

匿名网友

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

确定