gpt4 book ai didi

javascript - 单击文本时如何来回交换文本?

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:28 25 4
gpt4 key购买 nike

我需要的是通过单击将文本 ____(一个间隙)来回交换到 Word。我不想有一个按钮。用户应该只需要点击间隙的位置。

我看到 this page准确描述了我需要的东西,尤其是 CSS-Only Way,但是当我在 TryIt Editor 上尝试它时,它或 jQuery-Way 都不起作用。

这是 CSS 方式:

</style>
#example {
position: relative;
}
#example-checkbox {
display: none;
}
#example-checkbox:checked + #example:after {
content: "Hide";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: white;
}
</style>

<input id="example-checkbox" type="checkbox">
<label for="example" id="example">Show</label>

最佳答案

这是满足您要求的代码。

<!DOCTYPE html>
<html>

<body>
This is a paragraph. Click on the next word -&gt; <span id="word" onclick="toggle()">____</span> &lt;- Have you clicked on previous word.

<p id="demo"></p>

<script>
function toggle() {
var word = document.getElementById("word").innerHTML;
if (word.split('_').join('').trim().length === 0) {
document.getElementById("word").innerHTML = "word";
} else {
document.getElementById("word").innerHTML = "_____";
}
}
</script>
</body>

</html>


有多个隐藏词

在下面的代码中,只需看一下 <head> 中的脚本部分代码。

  <script>
var words = [];

words.push('vocabulary');
words.push('lexicon');
</script>

在这里,您只需按下要切换的字词 _____在数组中 words .一旦单词被插入这个数组,它们就会自动转换为下划线。只需将段落中的任何不同单词压入此数组,然后您自己查看转换。

span {
color: red
}
<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var words = [];

words.push('vocabulary');
words.push('lexicon');
</script>

</head>

<body>

<p id="demo">A vocabulary is a list of words that an individual knows or uses regularly. vocabulary is different from lexicon because vocabulary is about what an individual or group of people know, whereas lexicon is about the language itself.
</p>

<script>
function toggle(element) {
if (element.innerHTML.split('_').join('').trim().length === 0) {
element.innerHTML = element.getAttribute("word");
} else {
element.innerHTML = "_______";
}
}

$.each(words, function(index, value) {
var replacestr = new RegExp(value, "g");
$("p#demo:contains('" + value + "')").html(function(_, html) {
return html.replace(replacestr, ' <span class = "smallcaps" word="' + value + '" onclick="toggle(this)"> ' + value + ' </span>')
});
});
</script>
</body>

</html>


制作________页面加载时首先出现

只是替换

return html.replace(replacestr, ' <span class = "smallcaps" word="' + value + '" onclick="toggle(this)"> ' + value + ' </span>')

return html.replace(replacestr, ' <span class = "smallcaps" word="' + value + '" onclick="toggle(this)">____________</span>')

最终代码将是:

span {
color: red
}
<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var words = [];

words.push('vocabulary');
words.push('lexicon');
</script>

</head>

<body>

<p id="demo">A vocabulary is a list of words that an individual knows or uses regularly. vocabulary is different from lexicon because vocabulary is about what an individual or group of people know, whereas lexicon is about the language itself.
</p>

<script>
function toggle(element) {
if (element.innerHTML.split('_').join('').trim().length === 0) {
element.innerHTML = element.getAttribute("word");
} else {
element.innerHTML = "_______";
}
}

$.each(words, function(index, value) {
var replacestr = new RegExp(value, "g");
$("p#demo:contains('" + value + "')").html(function(_, html) {
return html.replace(replacestr, ' <span class = "smallcaps" word="' + value + '" onclick="toggle(this)">_______</span>')
});
});
</script>
</body>

</html>


对于复数和单数

只需更改行:

var replacestr = new RegExp(value, "g");

到:

var replacestr = new RegExp('\\b'+value+'\\b', "g");

最终代码如下所示:

span {
color: red
}
<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var words = [];

words.push('vocabulary');
words.push('lexicon');
words.push('lexicons');

</script>

</head>

<body>

<p id="demo">A vocabulary is a list of words that an individual knows or uses regularly. vocabulary is different from lexicon because vocabulary is about what an individual or group of people know, whereas lexicon is about the language itself. In this paragraph, lexicons is a new word that's added, so don't forget to push 'lexicons' in your array.
</p>

<script>
function toggle(element) {
if (element.innerHTML.split('_').join('').trim().length === 0) {
element.innerHTML = element.getAttribute("word");
} else {
element.innerHTML = "_______";
}
}

$.each(words, function(index, value) {
var replacestr = new RegExp('\\b'+value+'\\b', "g");
$("p#demo:contains('" + value + "')").html(function(_, html) {
return html.replace(replacestr, ' <span class = "smallcaps" word="' + value + '" onclick="toggle(this)">_______</span>')
});
});
</script>
</body>

</html>

关于javascript - 单击文本时如何来回交换文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38805892/

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