【发布时间】:2014-09-17 09:34:19
【问题描述】:
所以我正在使用 Node.JS 并做一个小型多人项目。
当然,我在很多事情上都使用 JavaScript。
我想检查 div 是否包含某个单词,以及该单词是否与变量匹配。
我将如何使用 JavaScript 做到这一点?
我想要类似的东西:
var teststring = "hi";
if "<div id="test">" contents matches "teststring"
{
}
我也有 jQuery,所以这也是一个选项!
我是 JavaScript 新手,所以请开火!
这是我的尝试,但没有成功:
var socket = io.connect('http://*');
socket.on('field', function (data) {
if ($("#userid").text().indexOf(data) > -1)
{
window.alert('lol');
console.log(data);
$("#field").html(data);
}
else
{
window.alert("Something has gone wrong with the node server...");
}
});
【问题讨论】:
-
if( document.getElementById('test').textContent.indexOf(teststring) > -1)应该这样做... -
这是在浏览器中还是在 Node 中?我猜它在客户端,只是不知道你为什么提到 Node ?
-
if ($("#test:contains('" + teststring + "')").length) {}
标签: javascript jquery node.js