外部JavaScript无输出问题排查:附HTML及JS代码
Troubleshooting Your Multiplication Script
Alright, let's fix this up for you—there are a couple of straightforward issues in your JavaScript code that are preventing the button from updating the page as expected:
Key Issues in demo.js
- Variable Overwrite & Logical Mix-Up: You first calculate the product
z = x * y, but then immediately re-declarezand try to assign the paragraph's content to it. This completely discards your calculation and does nothing to update the page. You need to reverse this logic: set the paragraph's content to your calculated product instead. - Case-Sensitive Property Name: JavaScript is case-sensitive, and the DOM property to update an element's content is
innerHTML(note the uppercaseHTML). You wroteinnerhtml(all lowercase), which JS doesn't recognize as a valid property.
Corrected demo.js Code
function multiply() { var x, y, z; x = 5; y = 10; z = x * y; // Fix the property name and reverse the assignment logic document.getElementById('exp').innerHTML = z; }
Quick Notes
Your HTML code is perfectly fine—since the JS file is in the same directory, the script tag's path works correctly. After updating demo.js with the code above, clicking the button will replace the "Multiplication result" text with 50, which is the product of 5 and 10.
内容的提问来源于stack exchange,提问作者Hashi
相关产品推荐
相关产品推荐

