好的,所以我希望制作一个脚本,如果此代码包含确切的单词“is123”,则类“a”将被添加到div id“#1”,问题是,“:contains”将无效完全包含整个字符串。我做了研究并找到了答案,但没有一个我能够理解并适用于我的情况!谢谢!
div id 1是倒计时,每秒更改一次。
<VirtualHost hostname:80>
SetEnv mysqllogin user
SetEnv mysqlpassword secret
</VirtualHost>
答案 0 :(得分:3)
<div id="1">is123</div>
<script>
if ($("#1").text().trim() === "is123") {
$("#1").addClass('a');
}
</script>
你需要把你的脚本放在元素之后,如果它在元素之前,它就找不到元素,因为DOM还没有渲染它。除非你把它包装在'jQuery document ready'函数中。
已编辑以反映对OP的更改
<div id="1">is123</div>
<script>
if ($("#1").text().trim() === "is123") {
$(".2").removeClass('hidden');
}
</script>
答案 1 :(得分:1)
使用此:
$("#1").text()==="is123"
===
表示完全匹配
代码:
<div id="1">is123</div>
<script>
if ($("#1").text()==="is123"){
$("#1").addClass('a');
}
</script>
答案 2 :(得分:0)
如果你想在计时器到达某一点时触发一个事件,你可以通过几种方式进行。
示例1
如果您有权访问该代码,最好挂钩到计时器功能本身。这可能是最有效的方法:
// This runs immediately when timer is updated
function checkTime(timerValue) {
if (timerValue == '01:05') {
$('.alert').show();
}
}
// SIMULATE YOUR TIMER - Courtesy of http://stackoverflow.com/a/20618517/1767412
function startTimer(duration, display) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (diff <= 0) {
start = Date.now() + 1000;
}
// Call your function to check the time
checkTime(display.textContent);
};
timer();
setInterval(timer, 1000);
}
window.onload = function() {
var display = document.querySelector('#timer');
startTimer(70, display);
};
.alert {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="timer"></div>
<div class="alert">Time to show this div!</div>
<div class="console"></div>
示例2
或者您可以执行以下操作,即按时间间隔轮询<div>
内容,并在符合条件时触发事件。我不喜欢这个,因为你有一个额外的进程以高频率运行 - 但在很多情况下这可能是唯一的选择:
/*
This runs once every 0.1 seconds so it will trigger
almost immediately after the timer reaches 01:05
*/
var t = setInterval(function() {
var timerValue = $("#timer").text().trim();
if (timerValue == '01:05') {
// Stop watching the timer once it matches
clearInterval(t);
// Show the div
$(".alert").show();
}
}, 100);
/*
SIMULATE YOUR TIMER
Courtesy of http://stackoverflow.com/a/20618517/1767412
*/
function startTimer(duration, display) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (diff <= 0) {
start = Date.now() + 1000;
}
};
timer();
setInterval(timer, 1000);
}
window.onload = function() {
var display = document.querySelector('#timer');
startTimer(70, display);
};
.alert {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="timer"></div>
<div class="alert">Time to show this div!</div>
答案 3 :(得分:-1)
试试这个
<script>
if ($(#1).text() === "is123";).addClass('a'))
</script>
<div id="1">is123</div>