我正在寫一個簡單的JavaScript庫來更新基於HTML5輸入範圍滑塊的元素中顯示的文本數量(即單詞數量)。根據輸入範圍更新顯示的文本量
我有兩個函數執行算法,但它們不顯示在DOM上。
HTML
<p>Text example:</p>
<section data-range="true">
<input type="range" value="90">
<p>This is some sample text that you can reduce. User Account Control (UAC) is a feature that can help you stay in control of your computer by informing you when a programs makes a change that requires administrator permissions. This can be helpful for several reasons, as it allows the user to view program changes and detect system corruption at a faster time.</p>
</section>
JS
/* getInnerText
* Get inner text of range section
* @param nested child <p> element(s) of range section x
* @return array of words in the paragraph
*/
function getInnerText(el) {
var p = el;
var innerText = p.innerText;
return innerText.split(' ');
}
/* updateInnerText
* Update inner text based on range section
*/
function updateInnerText(el) {
var rangeValue = el.srcElement.value;
var arrayOfWords = getInnerText(el.srcElement.parentNode.children[1]);
// update words to range value
arrayOfWords.splice(arrayOfWords.length, arrayOfWords.length - rangeValue);
// update values on DOM
el.srcElement.parentNode.children[1].innerText = arrayOfWords.join(" ");
}
這裏是一個JSbin。
只是好奇,但爲什麼你使用innerText而不是.text()? – yardpenalty
只是測試類型=範圍...如何顯示滑塊上的點的實際值? – yardpenalty
我只是發佈的答案,我希望它可能會幫助你 –