[英]Check if li contains the specified string
我有一个数组“名称”。 我需要检查此数组中的元素是否存在于html列表中。
php中的代码如下:
foreach ($name as $value2) {
echo "<script>
if($('li:not(:contains(\"".$value2."\"))'))
{
alert('".$value2." does not exists');
}
else
{
alert('".$value2." exists');
}
</script>";
}
但是我收到警报,因为并非每个元素都存在该值。
您可以使用PHP Arrays:
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
您可以这样尝试:
<?php foreach ($name as $value2) { ?>
<script>
if($('li:not(:contains(\"" <?php echo $value2; ?>"\"))'))
{
alert('"<?php echo $value2; ?>" does not exists');
}
else
{
alert('"<?php echo $value2; ?>" exists');
}
</script>
<?php } ?>
假设您无法在服务器上测试html的内容(例如在ajax之后),则创建它不是一个好习惯
<script>
.
</script>
<script>
.
</script>
<script>
.
</script>
什么时候可以做
$jsString = implode("','", $array);
$jsString = "'".$jsString."'";
?>
<script>
var vals = [<? echo $jsString; ?>];
var contains =[],notContains=[],liContains=[];
$.each(vals,function(i,val) {
var li = $('li:contains("'+val+'")');
if (li) {
contains.push(val); // all vals presents
liContains.push(li); // all (sets of) LIs with the vals
}
else {
notContains.push(val); // all vals not present
}
});
alert(contains.length);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.