Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取、累加和写入所有值​;对某一类的元素_Javascript_Html_Jquery - Fatal编程技术网

Javascript 获取、累加和写入所有值​;对某一类的元素

Javascript 获取、累加和写入所有值​;对某一类的元素,javascript,html,jquery,Javascript,Html,Jquery,我有这样一个代码: <div class='true' value='1'/> <div class='false' value='2'/> <div class='true' value='3'/> <div id='check'>Check my score</div> Your score is: <div id='total'/> 检查我的分数 你的分数是: document.getElementById('c

我有这样一个代码:

<div class='true' value='1'/>
<div class='false' value='2'/>
<div class='true' value='3'/>
<div id='check'>Check my score</div>
Your score is: <div id='total'/>

检查我的分数
你的分数是:

document.getElementById('check').addEventListener('click',function(){
设和=0;
document.querySelectorAll('.true').forEach(函数(el){
sum+=parseInt(el.getAttribute('value'));
});
document.getElementById('total')。innerHTML=sum;
});

检查我的分数

你的分数是:
首先,这是错误的:

<div class='true' value='1'/>

检查我的分数

您的分数是:
您需要通过
attr()
函数获取value属性,并需要为total创建sum变量,如下所示

function checkscore(){
  // where I was stuck to add all the values
  // I can only get the value and write it in the console
        var sum = 0;
        var values = $('.true').map(function(i,v) {

            var acc = parseInt($(v).attr('value'));
            sum += acc;
            //return acc;
        }).get(); 
        console.log( sum );
        $('#total').text(sum);
};
函数检查分数(){
//在那里我不得不添加所有的值
//我只能获取值并将其写入控制台
var总和=0;
变量值=$('.true').map(函数(i,v){
var acc=parseInt($(v).attr('value');
sum+=acc;
//返回acc;
}).get();
控制台日志(总和);
元("总计").正文(总和);;
};

检查我的分数

你的分数是:
到目前为止你尝试了什么?请展示你尝试了什么。好的,这就是我要找的,非常感谢!
function checkscore(){
  // where I was stuck to add all the values
  // I can only get the value and write it in the console
        var sum = 0;
        var values = $('.true').map(function(i,v) {

            var acc = parseInt($(v).attr('value'));
            sum += acc;
            //return acc;
        }).get(); 
        console.log( sum );
        $('#total').text(sum);
};