简体   繁体   English

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

[英]Why doesn't this jQuery css() call work?

I need not show a div when in a php if structure. 我不需要在php if结构中显示div。 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? 但是它不起作用,div显示它是否无关紧要,如果...我在做什么错?

Thanks a million 太感谢了

Instead of putting in JavaScript, why don't you do something like this: 而不是使用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>';
}

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>";

    }
    ?>

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>

Probably becuase your script runs before the div is loaded. 可能是因为您的脚本在加载div之前运行。 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>";
?>

I think hide is the best. 我认为皮革是最好的。 Leave css to your css files? 将CSS留给您的CSS文件?

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

edit: and your javascript too! 编辑:和你的JavaScript呢!

Why are you using jQuery to try and accomplish that? 您为什么要使用jQuery尝试做到这一点? Just use CSS. 只需使用CSS。

Or, at the very least, use jQuery's "live" to address the HIDE/SHOW of the DIV. 或者,至少,使用jQuery的“实时”解决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. 执行顺序很重要,而且我认为PHP生成的脚本实际上不会影响您正在构建的潜水并在有条件的条件下显示。

Your jQuery call is beign done before the element that will receive the click function is printed on the page. 您的jQuery调用已完成,将在页面上打印将接收click函数的元素。

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. 如果要使用未注释的呼叫,则需要在div标签之后打印它。

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

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

For starters, you don't actually use jQuery to change PHP. 首先,您实际上并没有使用jQuery来更改PHP。 jQuery is client-side, PHP is server-side, and never the twain shall meet. jQuery是客户端,PHP是服务器端,永远都不会满足。 What actually happens is that you use PHP to create your HTML document, and the browser will create a DOM that jQuery will manipulate. 实际发生的情况是,您使用PHP创建了HTML文档,浏览器将创建jQuery将操纵的DOM。

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. 您正在使用jQuery以非常“普通的Javascript”方式修改DOM。
    Instead of changing CSS, use jQuery's .hide() call. 不用更改CSS,而是使用jQuery的.hide()调用。
  2. You're alternating between single and double-quotes in your PHP echoes 您在PHP回声中交替使用单引号和双引号
    You use $( '.delete' ).css( "display", "none"); 您使用$( '.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. 当然,JavaScript是最常见的-但是最好明确指定脚本语言。
  4. You're using implied semi-colons 您正在使用隐含分号
    This can cause unexpected behavior in certain situations and should be avoided. 在某些情况下,这可能导致意外的行为,应避免使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM