Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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 有什么方法可以将数据放入JS函数中吗?_Javascript_Html - Fatal编程技术网

Javascript 有什么方法可以将数据放入JS函数中吗?

Javascript 有什么方法可以将数据放入JS函数中吗?,javascript,html,Javascript,Html,这是一个从firebase获取两个值的函数。我想要一个进度条,它将在从firebase检索到任何数据后立即工作 function toggleVideoState(fvalues, fthreshold) { const threshold = window.document.getElementsByClassName("threshold"); var progressBar = window.document.getElementsByClassName("progress-bar

这是一个从firebase获取两个值的函数。我想要一个进度条,它将在从firebase检索到任何数据后立即工作

function toggleVideoState(fvalues, fthreshold) {
  const threshold = window.document.getElementsByClassName("threshold");
  var progressBar = window.document.getElementsByClassName("progress-bar");
  var progressContainer = window.document.getElementsByClassName( "progress-container");
  var live=window.document.getElementsByClassName("live");
  let thresholdValue, value;
但有一个问题是,如果我通过设备从firebase获取值,这意味着没有来自用户的输入值,那么这个addEventListener函数不起作用。我想要另一种方法

document.addEventListener("change", e => {
if (e.target.className.indexOf('threshold') > -1) {
  setProgressBarValues(e.target, 'threshold');
} else if (e.target.className.indexOf('live') > -1) {
  setProgressBarValues(e.target, 'live');

}
});
for (var i = 0; i < 1; i++) {
var headdiv = document.getElementById('headdiv');
var elem = document.querySelector('#mycontainer');
var clone = elem.cloneNode(true);
clone.id = 'mycontainer' + i;
headdiv.appendChild(clone);
}
function setProgressBarValues(changedElement, changedElemName) {
var parent = changedElement.parentElement;
var thresholdValue, liveValue;

if (changedElemName === 'threshold') {
    thresholdValue = changedElement.value;
    liveValue = parent.querySelector('.live').value;
} else if (changedElemName === 'live') {
   thresholdValue = parent.querySelector('.threshold').value;
   liveValue = changedElement.value;
}

if (thresholdValue && liveValue) {
    const progressBar = parent.querySelector(".progress-bar");
    const progressContainer = parent.querySelector(".progress-container");

    let percentValue = Math.floor((liveValue / (2 * thresholdValue)) * 100);
    let percentMargin = Math.floor((25 * liveValue) / 100);
    console.log(percentValue, percentMargin);
    if (liveValue < 100) {
       progressBar.style.height = `calc(${liveValue}% - ${percentMargin}px)`;
    } else if (liveValue => 100) {
       progressBar.style.height = `calc(100% - 25px)`;
    } else {
      progressBar.style.height = `0px`;
    }
    if (percentValue < 50) {
      progressBar.style.backgroundColor = "red";
      progressContainer.style.borderColor = "red";
    } else {
      progressBar.style.backgroundColor = "green";
      progressContainer.style.borderColor = "green";
    }
}
}

}
document.addEventListener(“更改”,e=>{
if(e.target.className.indexOf('threshold')>-1){
setProgressBarValues(例如,目标“阈值”);
}else if(e.target.className.indexOf('live')>-1){
setProgressBarValues(如目标“live”);
}
});
对于(变量i=0;i<1;i++){
var headdiv=document.getElementById('headdiv');
var elem=document.querySelector(“#mycontainer”);
var clone=elem.cloneNode(真);
clone.id='mycontainer'+i;
头分区附加子项(克隆);
}
函数setProgressBarValues(changedElement,changedElemName){
var parent=changedElement.parentElement;
var阈值,liveValue;
如果(changedElemName=='threshold'){
thresholdValue=changedElement.value;
liveValue=parent.querySelector('.live').value;
}else if(changedElemName==='live'){
thresholdValue=parent.querySelector('.threshold').value;
liveValue=changedElement.value;
}
if(thresholdValue&&liveValue){
const progressBar=parent.querySelector(“.progressBar”);
const progressContainer=parent.querySelector(“.progressContainer”);
让percentValue=数学地板((liveValue/(2*thresholdValue))*100);
让percentMargin=Math.floor((25*liveValue)/100);
console.log(百分比值,百分比值);
if(liveValue<100){
progressBar.style.height=`calc(${liveValue}%-${percentMargin}px)`;
}else if(liveValue=>100){
progressBar.style.height=`calc(100%-25px)`;
}否则{
progressBar.style.height=`0px`;
}
如果(百分比值<50){
progressBar.style.backgroundColor=“红色”;
progressContainer.style.borderColor=“红色”;
}否则{
progressBar.style.backgroundColor=“绿色”;
progressContainer.style.borderColor=“绿色”;
}
}
}
}
html代码:

这是我必须删除的两个输入框,以便从firebase获取值而不是userinput

<input
      class="threshold"
      placeholder="threshold value"
      type="text"
      style="width: 120px; margin-top: 30px;margin-left: 0%; padding: 10px;" 
      />

    <input
      class="live"
      placeholder="live value"
      type="text"
      style="width: 120px; margin-bottom: 20px;padding: 10px;"
     />


简而言之,我想制作一个实时进度条,从firebase中获取其值。查看您的代码,一开始您似乎在玩多个值,但现在您只有一组值

所以在这种情况下,你应该这样做

function toggleVideoState(fvalues, fthreshold) {
   const progressBar = document.querySelector(".progress-bar");
   const progressContainer = document.querySelector(".progress-container");

   let percentValue = Math.floor((fvalues/ (2 * fthreshold)) * 100);
   let percentMargin = Math.floor((25 * fvalues) / 100);
   console.log(percentValue, percentMargin);
   if (fvalues < 100) {
      progressBar.style.height = `calc(${fvalues}% - ${percentMargin}px)`;
   } else if (fvalues=> 100) {
      progressBar.style.height = `calc(100% - 25px)`;
   } else {
      progressBar.style.height = `0px`;
   }
   if (percentValue < 50) {
     progressBar.style.backgroundColor = "red";
     progressContainer.style.borderColor = "red";
   } else {
     progressBar.style.backgroundColor = "green";
     progressContainer.style.borderColor = "green";
   }
 }
功能切换视频状态(Fvalue,fthreshold){
const progressBar=document.querySelector(“.progressBar”);
const progressContainer=document.querySelector(“.progressContainer”);
设百分比值=数学下限((fvalues/(2*fthreshold))*100);
设百分比裕度=数学下限((25*F值)/100);
console.log(百分比值,百分比值);
如果(Fv值<100){
progressBar.style.height=`calc(${fvalues}%-${percentMargin}px)`;
}否则如果(fvalues=>100){
progressBar.style.height=`calc(100%-25px)`;
}否则{
progressBar.style.height=`0px`;
}
如果(百分比值<50){
progressBar.style.backgroundColor=“红色”;
progressContainer.style.borderColor=“红色”;
}否则{
progressBar.style.backgroundColor=“绿色”;
progressContainer.style.borderColor=“绿色”;
}
}

或者在问题中指定在函数中获得的虚拟输入
toggleVideoState
。还有进度条的html结构。

如果我没有弄错的话,情况是这样的,您有两个输入字段,使用这些字段的值,您的函数可以正常工作。但现在,您正在从firebase获取值,并且希望您的函数应该以与firebase值相同的方式工作。我说的对吗?是的,你说的对再次感谢Kenny!现在开始工作了。是的,输入值只是1-100之间的数字。非常感谢!!