I am trying to wrap each line of a paragraph into a span tag. That works so far (thanks to the code I found). Now I am trying to bring in conditions. What I want is to check if the string (o a certain line) contains a certain text or pattern and if so, I would like to manipulate the output.
我试图将段落的每一行包装成span标记。这到目前为止(感谢我找到的代码)。现在我想引入条件。我想要的是检查字符串(某一行)是否包含某个文本或模式,如果是,我想操纵输出。
var txt = $(".ym-col1 p.address").html().split("<br>")
var output = ""
for(var x=0;x<txt.length;x++) {
if($("txt[x]:contains(\"08\")")) {
output = output + "<span class=\'telephone\'>"+txt[x]+"</span><br>"
}
if($("txt[x]:contains(\"@\")")) {
output = output + "<span class=\'email\'>"+txt[x]+"</span><br>"
}
}
$(".ym-col1 p.address").html(output);
If I only use the output with the if() it works but with the if I get each line of my text double. Any ideas what I am doing wrong?
如果我只使用带有if()的输出它可以工作但是如果我得到我的文本的每一行加倍。我有什么想法我做错了吗?
And another question. :) Is there a smart way to check if a string contains more than 5 digits? (Example: 123456)
另一个问题。 :)是否有一种聪明的方法来检查字符串是否包含超过5位数? (例如:123456)
2 个解决方案
#1
1
You are misusing jQuery in the if
statements, you should use indexOf
method of the String object:
你在if语句中滥用jQuery,你应该使用String对象的indexOf方法:
$(".ym-col1 p.address").html(function(i, html){
var txt = html.split('<br>'), output;
for(var x = 0; x < txt.length; x++) {
if ( txt[x].indexOf("08") > -1 )
output += "<span class='telephone'>"+txt[x]+"</span><br>"
else
output += "<span class='email'>"+txt[x]+"</span><br>"
}
return output;
})
#2
0
The following code creates a jQuery object (aka a wrapped set):
以下代码创建一个jQuery对象(也称为包装集):
$("txt[x]:contains(\"08\")")
It will always create a wrapped set, even if the wrapped set is empty, so the following conditional is always true:
即使包装集为空,它也将始终创建一个包装集,因此以下条件始终为true:
if($("txt[x]:contains(\"08\")")) {
If you want to check if a wrapped set is not empty, you can use:
如果要检查包装集是否为空,可以使用:
if($("txt[x]:contains(\"08\")").length > 0) {
But if what is between the <br>
tags is just text, you shouldn't even be using a wrapped set. You could use something line this:
但是如果
标签之间的内容只是文本,那么您甚至不应该使用包装集。您可以使用以下内容:
if (txt[x].indexOf("08") >= 0) {
and
和
if (txt[x].indexOf("@") >= 0) {
#1
1
You are misusing jQuery in the if
statements, you should use indexOf
method of the String object:
你在if语句中滥用jQuery,你应该使用String对象的indexOf方法:
$(".ym-col1 p.address").html(function(i, html){
var txt = html.split('<br>'), output;
for(var x = 0; x < txt.length; x++) {
if ( txt[x].indexOf("08") > -1 )
output += "<span class='telephone'>"+txt[x]+"</span><br>"
else
output += "<span class='email'>"+txt[x]+"</span><br>"
}
return output;
})
#2
0
The following code creates a jQuery object (aka a wrapped set):
以下代码创建一个jQuery对象(也称为包装集):
$("txt[x]:contains(\"08\")")
It will always create a wrapped set, even if the wrapped set is empty, so the following conditional is always true:
即使包装集为空,它也将始终创建一个包装集,因此以下条件始终为true:
if($("txt[x]:contains(\"08\")")) {
If you want to check if a wrapped set is not empty, you can use:
如果要检查包装集是否为空,可以使用:
if($("txt[x]:contains(\"08\")").length > 0) {
But if what is between the <br>
tags is just text, you shouldn't even be using a wrapped set. You could use something line this:
但是如果
标签之间的内容只是文本,那么您甚至不应该使用包装集。您可以使用以下内容:
if (txt[x].indexOf("08") >= 0) {
and
和
if (txt[x].indexOf("@") >= 0) {