为什么这个 jQuery css() 调用不起作用?
在 php if 结构中,我不需要显示 div。 我正在做:
<?php
$a = $_POST['somefiledcomingFromform'] ; //equals 1
if (isset($_POST['submit'])) {
if($a==1){
// echo"<script>$(document).ready(function() { $('.delete').css(\"display\", \"none\")});</script>";
echo"<script>$('.delete').css(\"display\", \"none\");</script>";
}
}
echo"<div class='delete'>Delete me</div>";
?>
但它不起作用,div显示我在 de if 内使用哪一行并不重要.. 我做错了什么?
感谢一百万
I need not show a div when in a php if structure.
I'm doing:
<?php
$a = $_POST['somefiledcomingFromform'] ; //equals 1
if (isset($_POST['submit'])) {
if($a==1){
// echo"<script>$(document).ready(function() { $('.delete').css(\"display\", \"none\")});</script>";
echo"<script>$('.delete').css(\"display\", \"none\");</script>";
}
}
echo"<div class='delete'>Delete me</div>";
?>
But it's not working, the div shows it does not matter what line I use inside de if ..
what am I doing wrong?
Thanks a million
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
与其放入 JavaScript,为什么不做这样的事情:
Instead of putting in JavaScript, why don't you do something like this:
试试这个
Try this
你可以这样做:
但我认为最好执行以下操作:
You can do:
But I think it is better to do the folowing:
可能是因为你的脚本在 div 加载之前运行。
试试这个:
Probably becuase your script runs before the div is loaded.
Try this:
我认为隐藏是最好的。将 css 留给您的 css 文件吗?
编辑:还有你的javascript!
I think hide is the best. Leave css to your css files?
edit: and your javascript too!
您为什么使用 jQuery 来尝试实现这一目标?只需使用 CSS 即可。
或者,至少,使用 jQuery 的“live”来解决 DIV 的 HIDE/SHOW。执行顺序很重要,我认为 PHP 生成的脚本实际上不会影响您在 PHP 条件之后构建和显示的潜水。
Why are you using jQuery to try and accomplish that? Just use CSS.
Or, at the very least, use jQuery's "live" to address the HIDE/SHOW of the DIV. The execution order matters, and I don't think the PHP generated script will actually affect the dive you're building and showing after the PHP conditional.
您的 jQuery 调用是在将接收单击函数的元素打印在页面上之前完成的。
您可以使用自己的注释行而不是直接调用。
如果您想使用未注释的调用,则需要在 div 标记之后打印它。
Your jQuery call is beign done before the element that will receive the click function is printed on the page.
You can use your own commented line instead the straight call.
If you want to use your uncommented call, you need to print it after your div tag.
对于初学者来说,您实际上并没有使用 jQuery 来更改 PHP。 jQuery 是客户端,PHP 是服务器端,两者永远不会相遇。实际发生的情况是,您使用 PHP 创建 HTML 文档,浏览器将创建 jQuery 操作的 DOM。
至于您的脚本有什么问题,您的代码不起作用的原因是因为语句按顺序回显。
以下内容不起作用:
以下内容起作用:
除了这个问题之外,还有一些“代码味道”会让我担心您的代码。请注意,这些在技术上都不是错误的,但它们往往表明其他问题。:
不要更改 CSS,而是使用 jQuery 的
.hide()
调用。您使用
$( '.delete' ).css( "display", "none");
...为什么?诚然,JavaScript 是最常见的 - 但最好还是明确指定脚本语言。
在某些情况下,这可能会导致意外行为,应该避免。
For starters, you don't actually use jQuery to change PHP. jQuery is client-side, PHP is server-side, and never the twain shall meet. What actually happens is that you use PHP to create your HTML document, and the browser will create a DOM that jQuery will manipulate.
As for what's wrong with your script, the reason your code does not work is because the statements are echoed in order.
The following does not work:
The following does work:
In addition to this problem, there are several "code smells" that would concern me about your code. Note that none of these are technically wrong, but they tend to indicate other issues.:
Instead of changing CSS, use jQuery's
.hide()
call.You use
$( '.delete' ).css( "display", "none");
... why?Granted, JavaScript is the most common - but it's still better to explicitly specify the script language.
This can cause unexpected behavior in certain situations and should be avoided.