简体   繁体   English

如何检查字符串是否包含特定的子字符串

[英]How to check if a string contains a specific substring

I'm working on a simple if/else jquery statement and I ran into some trouble with the variable. 我正在处理一个简单的if / else jquery语句,但该变量遇到了一些麻烦。 What I need it to do is to check if the var is true. 我需要做的是检查var是否为true。 In my case I want it to check if the has 'dont push' in it. 在我的情况下,我希望它检查其中是否有“不推动”功能。 If it's true, the html must change to 'lol'. 如果为true,则html必须更改为“ lol”。 If not, it will give a simple alert. 如果没有,它将给出一个简单的警报。 Can anybody give me some guidance here? 有人可以在这里给我一些指导吗?

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="user-scalable=no, width=device-width" />
<title>Untitled Document</title>
<link href="mobile.css" rel="stylesheet" type="text/css" media="only screen and (max-width: 480px)" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#nietklikken').click(function() {
        var n = $(this).html('dont push');
        if (n == "$(this).html('dont push')"){
            $(this).html('lol')     
        } else {
            alert('lolz');
        }
    });
});
</script>
</head>

<body>
<button type="button" id="nietklikken">dont push</button>

</body>
</html>

$(this).html() will get the innerhtml value and $(this).html(arg) will set the innerhtml value. $(this).html()get innerhtml值,而$(this).html(arg)set innerhtml值。 The correct usage will be below. 正确的用法如下。

$('#nietklikken').click(function() {
    if ($(this).html().indexOf('dont push') > -1){
        $(this).html('lol')     
    } else {
        alert('lolz');
    }
});

You should read up more in jquery docs . 您应该在jquery docs中阅读更多内容。

Update : It will now check if the innerhtml contains 'dont push'. 更新 :现在将检查innerhtml是否包含“不推送”。

You could use the ' indexOf ' operator that should tell you if a string contains another string. 您可以使用' indexOf '运算符,该运算符应告诉您一个字符串是否包含另一个字符串。 Try - 尝试-

if ($(this).html().indexOf('dont push') != -1){
            $(this).html('lol')     
        } else {
            alert('lolz');
        }

In general you could use a function like this 通常,您可以使用这样的函数

function HasSubstring(string,substring){
    if(string.indexOf(substring)>-1)
      return true;
      return false;
}

A working javascript code to check if string contains the specific word/substring: 一个有效的javascript代码,用于检查string是否包含特定的单词/子字符串:

var inputString = "this is my sentence";
var findme = "my";

if ( inputString.indexOf(findme) > -1 ) {
  out.print( "found it" );
} else {
  out.print( "not found" );
}

You can get the HTML content of an element by calling the html() method with no arguments like so: 您可以通过不带任何参数的html()方法来获取元素的HTML内容,如下所示:

var innerHtml = $(someElement).html();

You can then check for the presence of a string by using indexOf like so: 然后可以使用indexOf检查字符串是否存在,如下所示:

var position = "Find the string".indexOf("the");
// position = 5

If the given string isn't present, indexOf will return -1. 如果给定的字符串不存在, indexOf将返回-1。

var position = "Find the string".indexOf("notFound");
// position = -1

You can then use this in an if statement like so 然后,您可以在if语句中使用它,如下所示

if($(someElement).html().indexOf("dont push") >-1)
{
    // Required action here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM