为什么这个 jQuery css() 调用不起作用?

发布于 2024-12-07 23:01:30 字数 524 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

所谓喜欢 2024-12-14 23:01:30

与其放入 JavaScript,为什么不做这样的事情:

<?php

$a = 1;
if ($a == 1) {
    echo '<div class="delete" style="display:none;">Delete me</div>';

} else {
    echo '<div class="delete">Delete me</div>';
}

Instead of putting in JavaScript, why don't you do something like this:

<?php

$a = 1;
if ($a == 1) {
    echo '<div class="delete" style="display:none;">Delete me</div>';

} else {
    echo '<div class="delete">Delete me</div>';
}
離人涙 2024-12-14 23:01:30

试试这个

   <?php
    $a=1;
    if($a==1){
        echo"<div class='delete' style="display:none;">Delete me</div>";
    }
    else{
        echo"<div class='delete'>Delete me</div>";

    }
    ?>

Try this

   <?php
    $a=1;
    if($a==1){
        echo"<div class='delete' style="display:none;">Delete me</div>";
    }
    else{
        echo"<div class='delete'>Delete me</div>";

    }
    ?>
蝶舞 2024-12-14 23:01:30

你可以这样做:

<?php $a=1; if($a==1) : ?>

<script>
  $(document).ready(function() { 
    $('.delete').css("display", "none")
});
</script>
<?php endif; ?>

<div class='delete'>Delete me</div>

但我认为最好执行以下操作:

<?php $a = 1; ?>

<script>
  $(document).ready(function() { 
    $('.delete').css("display", "none")
});
</script>

<div class="<?php echo $a == 1 ? 'delete':'' ?>">Delete me</div>

You can do:

<?php $a=1; if($a==1) : ?>

<script>
  $(document).ready(function() { 
    $('.delete').css("display", "none")
});
</script>
<?php endif; ?>

<div class='delete'>Delete me</div>

But I think it is better to do the folowing:

<?php $a = 1; ?>

<script>
  $(document).ready(function() { 
    $('.delete').css("display", "none")
});
</script>

<div class="<?php echo $a == 1 ? 'delete':'' ?>">Delete me</div>
昔梦 2024-12-14 23:01:30

可能是因为你的脚本在 div 加载之前运行。
试试这个:

<?php
$a=1;
if($a==1){   

  //  echo"<script>$(document).ready(function() { $('.delete').css(\"display\", \"none\")});</script>";
   echo"<script>$(function(){$('.delete').css(\"display\", \"none\");});</script>";

}

echo"<div class='delete'>Delete me</div>";
?>

Probably becuase your script runs before the div is loaded.
Try this:

<?php
$a=1;
if($a==1){   

  //  echo"<script>$(document).ready(function() { $('.delete').css(\"display\", \"none\")});</script>";
   echo"<script>$(function(){$('.delete').css(\"display\", \"none\");});</script>";

}

echo"<div class='delete'>Delete me</div>";
?>
紙鸢 2024-12-14 23:01:30

我认为隐藏是最好的。将 css 留给您的 css 文件吗?

echo"<script>$('.delete').hide();</script>";

编辑:还有你的javascript!

I think hide is the best. Leave css to your css files?

echo"<script>$('.delete').hide();</script>";

edit: and your javascript too!

孤独患者 2024-12-14 23:01:30

您为什么使用 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.

烟花肆意 2024-12-14 23:01:30

您的 jQuery 调用是在将接收单击函数的元素打印在页面上之前完成的。

您可以使用自己的注释行而不是直接调用。

echo"<script>$(document).ready(function() { $('.delete').css(\"display\", \"none\")});</script>";

如果您想使用未注释的调用,则需要在 div 标记之后打印它。

<?php
echo"<div class='delete'>Delete me</div>";

$a=1;
if($a==1){   
echo"<script>$(function(){$('.delete').css(\"display\", \"none\");});</script>";
}
?>

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.

echo"<script>$(document).ready(function() { $('.delete').css(\"display\", \"none\")});</script>";

If you want to use your uncommented call, you need to print it after your div tag.

<?php
echo"<div class='delete'>Delete me</div>";

$a=1;
if($a==1){   
echo"<script>$(function(){$('.delete').css(\"display\", \"none\");});</script>";
}
?>
趁年轻赶紧闹 2024-12-14 23:01:30

对于初学者来说,您实际上并没有使用 jQuery 来更改 PHP。 jQuery 是客户端,PHP 是服务器端,两者永远不会相遇。实际发生的情况是,您使用 PHP 创建 HTML 文档,浏览器将创建 jQuery 操作的 DOM。

至于您的脚本有什么问题,您的代码不起作用的原因是因为语句按顺序回显。

以下内容不起作用:

<script>
    $('.delete').css("display","none");  // div.delete doesn't exist yet
</script>
<div class="delete">Delete me</div>      <!-- Output *after* the jQuery call. -->

以下内容起作用:

<script>
    $( function(){                       //wait until document.ready
        $( '.delete' ).hide();           //hide .delete after ready
    } );
</script>
<div class="delete">Delete me</div>      <!-- Output after <script> but will be hidden after document.ready -->

除了这个问题之外,还有一些“代码味道”会让我担心您的代码。请注意,这些在技术上都不是错误的,但它们往往表明其他问题。:

  1. 您正在使用 jQuery 以非常“简单的 Javascript”方式修改 DOM。
    不要更改 CSS,而是使用 jQuery 的 .hide() 调用。
  2. 您在 PHP 回显中交替使用单引号和双引号
    您使用 $( '.delete' ).css( "display", "none"); ...为什么?
  3. 您没有指定您正在使用的脚本类型
    诚然,JavaScript 是最常见的 - 但最好还是明确指定脚本语言。
  4. 您正在使用隐含的分号
    在某些情况下,这可能会导致意外行为,应该避免。

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:

<script>
    $('.delete').css("display","none");  // div.delete doesn't exist yet
</script>
<div class="delete">Delete me</div>      <!-- Output *after* the jQuery call. -->

The following does work:

<script>
    $( function(){                       //wait until document.ready
        $( '.delete' ).hide();           //hide .delete after ready
    } );
</script>
<div class="delete">Delete me</div>      <!-- Output after <script> but will be hidden after document.ready -->

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.:

  1. You're using jQuery to modify the DOM in a very "plain ol' Javascript" way.
    Instead of changing CSS, use jQuery's .hide() call.
  2. You're alternating between single and double-quotes in your PHP echoes
    You use $( '.delete' ).css( "display", "none"); ... why?
  3. You're not specifying the type of script you're using
    Granted, JavaScript is the most common - but it's still better to explicitly specify the script language.
  4. You're using implied semi-colons
    This can cause unexpected behavior in certain situations and should be avoided.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文