gpt4 book ai didi

Javascript将小写字母转换为大写字母

转载 作者:太空狗 更新时间:2023-10-29 14:44:21 28 4
gpt4 key购买 nike

下面的htmla和javascript代码有什么问题

formToConvert.html

<html>
<head>
<title>ExampleToConvert</title>
<script type = "text/javascript" src = "con.js"></script>
</head>
<body>
<form id ="myform">
<input type = "text" id = "field1" value = "Enter text Here"/><br/>
<input type ="submit" value = "submit" onclick = "convert()"/>
</form>
</body>
</html>

con.js

function convert() 
{
var str ;
str = document.getElementById("field1");
document.writeln(str.toUpperCase());
}

为什么上面的代码没有给我想要的结果?

最佳答案

尝试:

str = document.getElementById("field1").value;

这是因为 getElementById 返回对您的 HTML 元素的引用,而不是包含的“文本”值。

关于Javascript将小写字母转换为大写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7012114/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com