[英]How to add class to element that contains a certain string which is being pulled from database
I was using the following code to search for a particular piece of text within a div, and if found, to add another class to the div. 我使用以下代码在div中搜索特定文本,如果找到,则将另一个类添加到div。
<div class="res">Full HD</div>
$(document).ready(function () {
$('div.res').each(function () {
if ($(this).text() === "Full HD") {
$(this).addClass('fullhd');
}
});
});
It worked great and thank you to this community for helping me figure out how to do that. 它的运行效果非常好,感谢您对这个社区的帮助,以帮助我弄清楚该如何做。
However, I am instead pulling the text that I want the function to search for, ie "Full HD", from the database using the following PHP and now it no longer works, as I reluctantly expected. 但是,我改用下面的PHP从数据库中提取我想要函数搜索的文本,即“ Full HD”,但现在它不再起作用,正如我很不希望的那样。
<div class=\"res\">".$suggested_videos[$i]['definition']."</div>
I sort of understand why this no longer works but have a hard time articulating it so I apologize for that. 我有点理解为什么这种做法不再有效,但很难说清楚,因此我对此表示歉意。 Is there something I can add to the javascript function to search for "Full HD" within the ['definition'] column of my database and to then add the correct class ('fullhd') based upon if it finds it or not?
是否可以添加到javascript函数中,以在数据库的['definition']列中搜索“ Full HD”,然后根据是否找到正确的类('fullhd')添加正确的类?
EDIT: 编辑:
So the javascript function I was using was in fact working correctly with the php database and didn't need to be changed at all. 因此,我使用的javascript函数实际上可以与php数据库一起正常使用,根本不需要更改。 The issue was due to me not capitalizing "Full HD" on the database.
问题是由于我没有在数据库中大写“ Full HD”。 It was pulling "Full HD" from the database instead of "FULL HD", and thus when searching for "FULL HD", nothing was found, and thus no class was being added.
它是从数据库中提取“ Full HD”而不是“ FULL HD”,因此在搜索“ FULL HD”时,什么都没有找到,因此没有添加任何类。 Thank you for the replies and the potential workarounds.
感谢您的答复和潜在的解决方法。 It means a lot to know there are people out there willing to help a stranger.
知道有很多人愿意帮助一个陌生人,这意味着很多。 I can't wait to give back.
我等不及要回报了。
Since you are outputting your content via PHP anyhow, you could simply add a conditional on the server / PHP side checking for what $suggested_videos[$i]['definition']
is, and then add the class there. 由于无论如何都是通过PHP输出内容,因此只需在服务器/ PHP端添加一个条件,检查
$suggested_videos[$i]['definition']
是什么,然后在其中添加类。 See the following: 请参阅以下内容:
<?php
echo '<div class="res ';
if ($suggested_videos[$i]['definition'] === 'Full HD') {
echo 'fullhd';
}
echo '">' . $suggested_videos[$i]['definition'] .'"</div>';
?>
Untested but it should lead you down the correct path. 未经测试,但可以引导您走正确的道路。
Some further clarification: your JavaScript runs within the frontend of a website / in a user's browser, and has no direct access to the database. 需要进一步澄清:您的JavaScript在网站的前端/用户的浏览器中运行,并且无法直接访问数据库。 PHP runs on your server (even if it's your local development machine) and will generate the content to be output to the frontend, before the user even sees it.
PHP在您的服务器上运行(即使它是您的本地开发计算机),并会生成内容,并在用户看到之前将其输出到前端。 The above checks the content and adds the
<div>
class if the conditional is correct. 以上检查内容,如果条件正确,则添加
<div>
类。 This may also be of use to you: https://stackoverflow.com/a/6369454/823549 . 这也可能对您有用: https : //stackoverflow.com/a/6369454/823549 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.