代码之家  ›  专栏  ›  技术社区  ›  JackJack

我正在尝试将事件目标值传递给另一个函数

  •  0
  • JackJack  · 技术社区  · 7 年前

    const justFeet = totalFeet.addEventListener('input', (e) => {
        const heightInFeet = e.target.value;
        let displayFeet = document.createElement('h3')
        displayFeet.textContent = heightInFeet * 12
        // totalInches.appendChild(displayFeet)
    })
    
    const justInches = inches.addEventListener('input', (e) => {
        const addOnInches = e.target.value;
        let displayInches = document.createElement('h3')
        displayInches.textContent = addOnInches
        // totalInches.appendChild(displayInches)
    })
        
    function totalHeight (feet, inches) {
        const finalTotal = feet + inches;
        let finalHeight = document.createElement('h3')
        finalHeight.textContent = finalTotal
        totalInches.appendChild(finalHeight)
    }
    
    totalHeight(displayFeet, displayInches)
    1 回复  |  直到 7 年前
        1
  •  1
  •   Steve    7 年前

    <html>
    <style>
    </style>
    
        Feet:<input id="feet" type="number"></input>
        Inches:<input id="inches" type="number"></input>
        <h3>Feet converted to inches: <span id="displayFeetToInches"></span></h3>
        <h3>Inches: <span id="displayInches"></span></h3>
        <h3>Total inches:<span id="finalHeight"></span></h3>
    
        <script>
    
            const feet = document.getElementById("feet");
            const inches = document.getElementById("inches");
            const total = document.getElementById("total");  //in inches
            const displayFeetToInches = document.getElementById("displayFeetToInches");  //in inches
            const displayInches = document.getElementById("displayInches");  //in inches
    
            const justFeet = feet.addEventListener('input', (e) => {
                console.log('justFeet');
                const heightInFeet = e.target.value;
                displayFeetToInches.textContent = heightInFeet * 12;
                totalHeight();
            })
    
            const justInches = inches.addEventListener('input', (e) => {
                console.log('justInches');
                const addOnInches = e.target.value;
                displayInches.textContent = addOnInches
                totalHeight();
            })
    
            function totalHeight (feet, inches) {
                console.log('totalinches');
                const ftToInches = displayFeetToInches.textContent;
                const addOn = displayInches.textContent;
                const finalTotal = parseInt(ftToInches) + parseInt(addOn);
                finalHeight.textContent = finalTotal
            }
    
        </script>
    </html>
    
    推荐文章