【问题标题】:How to write php if statement in jquery script [closed]如何在jquery脚本中编写php if语句[关闭]
【发布时间】:2017-04-24 11:07:02
【问题描述】:

我需要一些关于我的代码的帮助。我如何在 jquery 脚本中使用 php if 语句? 没有 php 工作得很好,但无论如何我都需要这些条件。 那么有什么建议吗?

<script type="text/javascript">
$( document ).ready(function(){

$(<?php if(isset($banner_1)) :?>
   "<div class='banner1' id='banner_top' <?php if($banner_1->on_off == 0) :?>
    style='display: none' <?php endif ;?>>" +
   "<img  id='banner_top_img' class='img-responsive' 
    src='"<?=base_url('assets/img/'.$banner_1->userfile);?>"' alt='image'>" +
   "</div>" 
<?php endif ;?>).insertAfter($("#latest div:eq(2)"));
}
</script>

所以这是没有条件的工作

$( document ).ready(function(){

$(
"<div class='banner1' id='banner_top' >"+
"<img  id='banner_top_img' class='img-responsive' src='<?=base_url('assets/img/'.$banner_1->userfile);?>' alt='image'>" +
"</div>").insertAfter($("#latest div:eq(2)"));

});
</script>

【问题讨论】:

  • 您的要求是什么。你能解释一下吗?
  • 您的代码令人困惑。能否请您添加您的纯 JS 代码
  • '' + JS 代码
  • Adam Banga 我需要在#latest div:eq(2) 之后显示横幅,如果它打开的话。如果横幅关闭,我想成为 style='display: none'。
  • 这是一种非常丑陋的方式来做这样的事情。您应该考虑一些关注点分离

标签: javascript php jquery codeigniter


【解决方案1】:

这里有双引号:

src='"<?=base_url('assets/img/'.$banner_1->userfile);?>"'

应该是

src='<?=base_url('assets/img/'.$banner_1->userfile);?>'

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2016-06-28
    • 2012-12-30
    相关资源
    最近更新 更多