[英]can anyone tell me why my javascript clicks isnt working?
i am making a shopping cart, looking to change the quantities but cant work out why its not working. 我正在制作购物车,希望更改数量,但无法弄清为什么它不起作用。 i have created two funtions, one to add quantities and one to take away.
我创建了两个功能,一个增加数量,另一个带走。 i want to apply this to multiple objects in my shopping cart.
我想将此应用到购物车中的多个对象。 it works if i change the p tag to an id rather than a class but i want it to apply to multiple items
如果我将p标签更改为一个id而不是一个类,它将起作用,但是我希望它应用于多个项目
<div class="controlpic">
<img class="img" src="jsbook1.png">
<button onClick="onClick2()">-</button>
<p class="clicks">0</p>
<button onClick="onClick()">+</button>
<button class="addBasket">Add to Basket</button>
<div>some text about this product</div>
</div>
/* onclick function will add clicks for basket*/
var clicks1 = 0;
function onClick() {
clicks1 += 1;
document.getElementsByClassName("clicks").innerHTML = clicks1;
}
/* onclick function will take away clicks for basket*/
var clicks1 = 0;
function onClick2() {
if (clicks1 > 0) {
clicks1 -= 1;
document.getElementsByClassName("clicks").innerHTML = clicks1;
}
}
The problem in your code is that getElementsByClassName
returns an HTMLCollection
, which is similar to (but not the same as) an array. 您的代码中的问题是
getElementsByClassName
返回一个HTMLCollection
,它类似于(但不等同于)数组。
Instead of changing the innerHTML
of your HTMLCollection
, you should change the innerHTML
the element(s) in your HTMLCollection
. 而不是更改
HTMLCollection
的innerHTML
,您应该更改innerHTML
HTMLCollection
中的HTMLCollection
。
If you only have one element with the class "clicks", you can just replace ... 如果您只有一个元素属于“ clicks”类,则可以替换...
document.getElementsByClassName("clicks").innerHTML = clicks1;
... with ... ...与...
document.getElementsByClassName("clicks")[0].innerHTML = clicks1;
... and everything will work just fine. ...一切都会正常进行。
If you need to support multiple elements, you'll have to loop over those elements : 如果需要支持多个元素,则必须遍历这些元素:
[].forEach.call(document.getElementsByClassName('clicks'), function(clicks) {
clicks.textContent = clicks1;
});
var clicks1 = 0; function onClick() { clicks1 += 1; [].forEach.call(document.getElementsByClassName('clicks'), function(clicks) { clicks.textContent = clicks1; }); } function onClick2() { if (clicks1 > 0) { clicks1 -= 1; [].forEach.call(document.getElementsByClassName('clicks'), function(clicks) { clicks.textContent = clicks1; }); } }
<div class="controlpic"> <img class="img" src="jsbook1.png"> <button onClick="onClick2()">-</button> <p class="clicks">0</p> <button onClick="onClick()">+</button> <button class="addBasket">Add to Basket</button> <div>some text about this product</div> </div>
it works if i change the p tag to an id rather than a class but i want it to apply to multiple items
如果我将p标签更改为一个id而不是一个类,它将起作用,但是我希望它应用于多个项目
It doesn't work, because you are trying to set property innerHTML
on an HTMLCollection that document.getElementsByClassName("clicks")
returns, instead of setting it on every of those elements. 这是行不通的,因为您尝试在
document.getElementsByClassName("clicks")
返回的HTMLCollection上设置属性innerHTML
,而不是在所有这些元素上进行设置。
Replace your document.getElementsByClassName("clicks").innerHTML = clicks1
with: 将您的
document.getElementsByClassName("clicks").innerHTML = clicks1
为:
[].forEach.call(document.getElementsByClassName('clicks'), function(element) {
element.textContent = clicks1;
});
Below demo contains many flaws, it only presents how to implement the above fix. 下面的演示包含许多缺陷,仅介绍如何实现上述修复。 Also, each quantity modifier (
+
or -
) will update all counters. 同样,每个数量修饰符(
+
或-
)将更新所有计数器。 For solution to this, see section Rewrite . 有关此问题的解决方案,请参见“ 重写”部分。
/* onclick function will add clicks for basket*/ var clicks1 = 0; function onClick() { clicks1 += 1; [].forEach.call(document.getElementsByClassName('clicks'), function(element) { element.textContent = clicks1; }); } /* onclick function will take away clicks for basket*/ var clicks1 = 0; function onClick2() { if (clicks1 > 0) { clicks1 -= 1; [].forEach.call(document.getElementsByClassName('clicks'), function(element) { element.textContent = clicks1; }); } }
<div class="controlpic"> <img class="img" src="jsbook1.png"> <button onClick="onClick2()">-</button> <p class="clicks">0</p> <button onClick="onClick()">+</button> <button class="addBasket">Add to Basket</button> <div>some text about this product</div> </div>
Few other notes about your code: 关于您的代码的其他几点注意事项:
onclick
use element.addEventListener('click', someFunction)
onclick
使用element.addEventListener('click', someFunction)
innerHTML
in your case, you can use textContent
. textContent
,而不是用innerHTML
。 It's safer and faster. var clicks1 = 0
second time makes no difference in your code. var clicks1 = 0
不会对您的代码造成任何影响。 function setQuantityModifier(element) { 'use strict'; var quantity = element.parentNode.getElementsByClassName('clicks')[0]; element.addEventListener('click', function() { 'use strict'; if (element.value === '+') { quantity.textContent = parseInt(quantity.textContent) + 1; } else if (parseInt(quantity.textContent) > 0) { quantity.textContent = parseInt(quantity.textContent) - 1; } }); } [].forEach.call(document.querySelectorAll('.controlpic > input'), setQuantityModifier);
<div class="controlpic"> <img class="img" alt="ADDME" src="jsbook1.png"/> <input type="button" value="–"/> <p class="clicks">0</p> <input type="button" value="+"/> <button class="addBasket">Add to Basket</button> <div>some text about this product</div> </div> <div class="controlpic"> <img class="img" alt="ADDME" src="jsbook2.png"/> <input type="button" value="–"/> <p class="clicks">0</p> <input type="button" value="+"/> <button class="addBasket">Add to Basket</button> <div>some text about this product</div> </div>
It doesn't work because document.getElementsByClassName
returns an array (as you see getElement s , which is plural). 它不起作用,因为
document.getElementsByClassName
返回一个数组(如您所见,getElement s是复数)。 You can either do document.getElementsByClassName('clicks')[0].innerHTML
or you can use document.querySelector('.clicks').innerHTML
您可以执行
document.getElementsByClassName('clicks')[0].innerHTML
,也可以使用document.querySelector('.clicks').innerHTML
EDIT: for updating multiple elements with class name, you can use (based on @tymeJV's comment): 编辑:使用类名更新多个元素,您可以使用(基于@tymeJV的注释):
var clicks = document.getElementsByClassName('clicks');
for(var i = 0; i < clicks.length; ++i) {
clicks[i].innerHTML = "...";
}
I personally like using document.querySelectorAll('.clicks')
for any matchings (whether it is a class, id, or attribute). 我个人喜欢将
document.querySelectorAll('.clicks')
用于任何匹配项(无论是类,id还是属性)。
This should work for you: https://jsfiddle.net/DIRTY_SMITH/7oe5kh9L/30/ 这应该为您工作: https : //jsfiddle.net/DIRTY_SMITH/7oe5kh9L/30/
Add an id to <p class="clicks" id="clicks">0</p>
向
<p class="clicks" id="clicks">0</p>
添加ID
And insert text with document.getElementById('clicks').innerHTML = clicks1;
并使用
document.getElementById('clicks').innerHTML = clicks1;
插入文本document.getElementById('clicks').innerHTML = clicks1;
Also remove the second var clicks1 = 0;
同时删除第二个
var clicks1 = 0;
because you only need to declare a variable once. 因为您只需要声明一次变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.