如果变量包含文本,则显示div

时间:2017-02-17 14:36:53

标签: php html css

我想检查变量是否包含*或**,然后我希望它根据找到的内容显示div。我写了一个不起作用的剧本。

我现在已将其更改为使用PHP并且它现在正在运行:

<?php

if (strpos($this->element->product_description, '*') !== false) {
    echo '<div class="asterisk">
    <ul class="asterix">
        <li>* Subject to receipt of payment and print ready artwork or sign off of proofs. Large quantities may require a longer lead time.</li>
        <li>** Custom sizes can be manufactured to order, longer lead times may apply.</li>
    </ul>
</div>';
};
?>

1 个答案:

答案 0 :(得分:1)

试试这个。使用javascript选择器

jQuery indexOf方法

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
    <div class="asterisk" style="display: none;">
        <ul class="asterix">
            <li>* Subject to receipt of payment and print ready artwork or sign off of proofs. Large quantities may require a longer lead time.</li>
            <li>** Custom sizes can be manufactured to order, longer lead times may apply.</li>
        </ul>
    </div>
    <script>
        if ($(".asterisk ul li").text().indexOf('*') > -1) {
            $('.asterisk').css('display', 'block');
        }
    </script>

</body>

</html>
&#13;
&#13;
&#13;