Javascript 使用 PHP 和 jQuery 更改 CSS 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14303780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing CSS styles with PHP and jQuery
提问by Kyle Yeo
I'm executing a PHP if/else statement.
我正在执行一个 PHP if/else 语句。
However, I want the code to do something like this :
但是,我希望代码做这样的事情:
<?php
if ($condition == true){
echo 'ITS TRUE!';
}else {
#id-element.css('display':'none');
}
?>
Note that the ELSE statement executes a jQuery line that changes the css style of the element involved.
请注意,ELSE 语句执行一个 jQuery 行,该行更改所涉及元素的 css 样式。
How do i go about doing this?
我该怎么做?
Anyone have any suggestions / examples of how I could implement a PHP if/else and change css styles with jQuery inside the PHP if/else statement?
任何人都有任何关于如何实现 PHP if/else 并在 PHP if/else 语句中使用 jQuery 更改 css 样式的建议/示例?
回答by Will
You can echo the HTML for a style element and throw the CSS inside that.
您可以回显样式元素的 HTML 并将 CSS 放入其中。
else {
echo '<style type="text/css">
#id-element {
display: none;
}
</style>';
}
回答by clean_coding
Reckon you could do something like this:
估计你可以做这样的事情:
<?php
if ($condition == true):
echo 'ITS TRUE!';
else:
?>
//put whatever html/style/script you want here, for example
<script>
$( '#id-element' ).css('display', 'none');
</script>
<?php endif; ?>

