我正在开发一个“所见即所得”的应用程序。您在一个框中编码,输出显示在另一个框中。我需要检查用户是否在 HTML 文本区域内键入了特定文本,如果它是正确的,则会显示一个按钮。 到目前为止,当用户键入 text-align:center;该按钮可见。我无法锻炼,所以用户必须输入 2 组文本。 到目前为止我有这个:
$(document).ready(function(){$(".textArea").keyup(function() { // directed at the textArea div tag
if ($(this).val().indexOf('text-decoration:underline;' && 'text-align:center;') != -1) { // if the text matches those 2 strings
$(".continue").css("visibility", "visible"); // make button visible
}
else {
$(".continue").css("visibility", "hidden"); // keep it hidden if strings haven't been produced
$(".correct").css("display", "block");
}
});
});
.continue{
background-color: #ef6d3b;
width: 6em;
text-align: center;
font-size: 15px;
border: none;
height: 25px;
color: #000000;
outline: none;
cursor: pointer;
border-radius: 5px;
text-transform: uppercase;
position: relative;
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div class="codeArea">
<div class="correct">
<textarea class="textArea">
<html>
<body>
</body>
</html>
</textarea>
</div>
</div>
<a href="task2.php"><button class="continue" type="button">Continue</button></a>
最佳答案
您在 if 语句中使用了错误的表达式..
if ($(this).val().indexOf('text-decoration:underline;' && 'text-align:center;') != -1)
计算结果与
相同$(this).val().indexOf('text-align:center;') != -1
你真正应该做的是
$(this).val().indexOf('text-decoration:underline;')!=-1 && $(this).val().indexOf('text-align:center;')!=-1
关于javascript - jQuery 使用 HTML textarea 检查多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36153420/