【问题标题】:get numbers from string and insert into css从字符串中获取数字并插入css
【发布时间】:2022-02-13 00:09:52
【问题描述】:

我正在开发一个在名为 Intrexx 的开发平台上使用的 javascript 函数。它有其自身的局限性,但我相信下面的代码应该可以工作。 它应该在数组中获取数字,根据它找到的内容计算百分比并返回该百分比以设置进度条的宽度。除了更改设计部分外,一切都按预期工作。任何想法为什么?

function progressBar(){
// get text of navigation (eg: 3 of 28) 
var textProgress = document.getElementById('ID_navigationinfocontrolAB63A4D8').firstElementChild.innerHTML

// get numbers inside above string as array
var a = textProgress.match(/\d+/g).map(Number)

// take numbers in array, calculate percentage of progress, return int
var progressPercentage = parseInt((a[0]*100)/a[1])

// alter design of progress bar
var progressBarDesign = document.getElementById('ID_separatorcontrolC6F8BAC8').parentNode
progressBarDesign.style.width = progressPercentage +'% !important' }

我尝试直接编辑元素,但无论是这个还是编辑 parentNode 都不起作用。

【问题讨论】:

    标签: javascript css intrexx


    【解决方案1】:

    要设置重要,您可以使用以下语法

     progressBarDesign.style.setProperty("width", progressPercentage + '%', "important");
    

    或者,如果您可以不使用important,则当前语法应该可以正常工作。

    progressBarDesign.style.width = progressPercentage + '%';
    

    【讨论】:

    • 谢谢,但不幸的是它不起作用。我设置了!important,所以它会否决任何其他设置,但是当我在控制台中设置宽度时,它可以工作。
    • 你能console.log(progressPercentage)看看这个值是否真的被正确计算了吗?
    • 计算正确,是的。我 console.log 编辑了所有变量,它们按预期显示。只有最后一行代码有问题
    【解决方案2】:

    终于设法让它与 jQuery 一起工作。我不知道为什么这行得通,而我的初始代码却没有,但希望它对其他人有所帮助。发布与第一次尝试不同的地方

        // alter design of progress bar 
        var progressBarDesign = 
        document.getElementById('ID_separatorcontrolC6F8BAC8').parentNode
    
        // initialize below function
        setCss(progressBarDesign, 'width', progressPercentage + '%')
        }
    
        function setCss(oHtmlButton, cssKey, cssValue){
        $(oHtmlButton).css(cssKey, cssValue);
        }
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多