gpt4 book ai didi

javascript - 通过 Javascript 将字母大写

转载 作者:行者123 更新时间:2023-11-28 04:28:10 26 4
gpt4 key购买 nike

HTML代码

<body>
<h2>Text Changer</h2>
<p>The following program will change the way your text looks.</p>

<p>Uppercase/Lowercase: <input id = "upperLower" type="checkbox" onclick = "capitalize()" /></p>

<p id = "lorem">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>

JS代码

'use strict';

$(document).ready(function() {
});

var str;
str = document.getElementById('lorem').value;

function capitalize() {
if (document.getElementById('upperLower').checked) {
document.writeln(str.toUpperCase());
}
if (document.getElementById('upperLower').unchecked) {
document.writeln(str.toLowerCase());
}
}

因此,使用这段代码,我试图通过选中/取消选中框来获取将 lorem 文本大写/取消大写的功能。但是,它似乎不起作用。谁能帮我解决这个问题?非常感谢,谢谢!

最佳答案

p 没有属性 value。您可以改用 textContentinnerText

改变

document.getElementById('lorem').value;

document.getElementById('lorem').innerText;

'use strict';
function capitalize() {
var str = document.getElementById('lorem').innerText;

if (document.getElementById('upperLower').checked) {
document.getElementById('lorem').innerText = str.toUpperCase();
}else{
document.getElementById('lorem').innerText = str.toLowerCase();
}
}
<h2>Text Changer</h2>
<p>The following program will change the way your text looks.</p>

<p>Uppercase/Lowercase: <input id="upperLower" type="checkbox" onclick="capitalize()" /></p>

<p id="lorem">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

关于javascript - 通过 Javascript 将字母大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47523910/

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