【问题标题】:jquery hasClass does't work when checking dynamically added class检查动态添加的类时jquery hasClass不起作用
【发布时间】:2013-08-22 06:13:19
【问题描述】:

我有一个问题,我通过 php 将一个类动态添加到 div,然后检查 id "checkLang" 是否具有空间类,但它不起作用:(

也许有人知道问题所在? 我的html:

<div id="checkLang" class ="<?php echo $valoda ?>"><div>

我检查的那个 div

if ($('#checkLang').hasClass('RU')){
    <?php include("lan/ru.php"); ?>
};

if ($('#checkLang').hasClass('LV')){
    <?php include("lan/lv.php"); ?>
};

我不知道为什么,但两个 if 都包含语言 php 文件。 但也许原因是因为这个脚本在带有标题&lt;?php header('Content-type: text/javascript'); ?&gt;的php文件中我在索引页面中附加了像javascript文件这样的文件,比如&lt;script type="text/javascript" src="php/FormuValidacija.php" /&gt;&lt;/script&gt;

我试过了

if($_GET['lang']=="latviesu"){
include("php/lan/lv.php");
}
else($_GET['lang']=="krievu"){
include("php/lan/ru.php");
}

但是效果不好:(

附:抱歉,如果这是个愚蠢的问题,我是新手,但愿意学习! :)

【问题讨论】:

  • 仅供参考,更常见的是,当客户端代码添加动态时,您会调用它。另外你为什么不在php中做if($valoda == 'RU')?
  • 我试过if($valoda == 'RU'),但效果不佳。

标签: php javascript jquery class


【解决方案1】:

看起来您的脚本没有添加到类似 dom 的处理程序中

jQuery(function($){
alert($('#checkLang').length)//it should alert 1 not 0
if ($('#checkLang').hasClass('RU')){
    <?php include("lan/ru.php"); ?>
};
})

【讨论】:

  • 不管条件如何都不会包含php文件吗?
  • 感谢您的建议,它会提醒 1,但双方条件仍会执行 :(
  • @KristīneGlode 你能提醒alert($('#checkLang').attr('class'))然后
  • 哦,当alert($('#checkLang').attr('class')) 我有正确的警报时 - 取决于页面语言!我会尝试使用该解决方案!
  • 哈,奇怪,但是当我检查两种语言的 if (($('#checkLang').attr('class')) == ('RU')) 时,两个 ifs 都会执行。
【解决方案2】:

当前语言:

<div id="checkLang" class ="<?php echo $valoda ?>"><div>

包括您的本地化文件:

<div class="ru" style="display:none;">
       <?php include("lan/ru.php"); ?>
</div>

<div class="lv"  style="display:none;">
        <?php include("lan/lv.php"); ?>
</div>

获取当前语言并显示对应的div

<script type="text/javascript">
    $(document).ready(function(){
        var currLng = $('#checkLang').attr('class'); // get current language, i.e class of #checkLang
        $('div.' + currLng).show(); // show div with corresponding class, i.e lang
    });
</script>

但是,最好在服务器端获取语言,而不是将未使用的文件加载到客户端(即 HTML)

P.S un labāk visus variablus, script failus 等 saukt anglū valodā :)

【讨论】:

    【解决方案3】:

    在这里给出我的 jsfiddle 示例,您可以清楚地看到 javascript 能够检查给定的类是否设置在 div 上。

    http://jsfiddle.net/n5e9a/

    无论如何,这两个 php 文件都会被渲染,因为这里的 if 检查是在客户端 javascript 上完成的。如果您希望在客户端完成检查后仅呈现 php 文件中的数据。然后你必须从客户端获取数据,而不是从服务器端提供数据。

    一种方法是通过 ajax。例如。您可以创建一个基于查询返回 html 内容的 php 脚本,例如:

    checklang.php?lang=RU

    在 javascript 代码中,您将有一个这样的请求:

    $.get('checklang.php?lang=RU', function(data) { //switch RU with a js variable so you can change between RU and LV programatically
      $('#some-content-div').html(data);
    });
    

    【讨论】:

      【解决方案4】:

      它包含您的内容,因为它已经在服务器端生成,因此当 html 站点被传输时,php 脚本已经被渲染,而 javascript 被处理客户端,您将需要调整您的尝试

      你知道这会起作用

      <div id="checkLang" class ="<?php echo $valoda ?>"><div>
      <div class="ru" style="display:none;">
             <?php include("lan/ru.php"); ?>
      </div>
      
      <div class="lv"  style="display:none;">
              <?php include("lan/lv.php"); ?>
      </div>
      
      
      $(document).ready(function()
       {
         if ($('#checkLang').hasClass('RU')){
             $('.ru').show();   
         }
      
      if ($('#checkLang').hasClass('LV')){
           $('.lv').show();   
      };             
       });
      

      这里的例子:

      http://jsfiddle.net/yVpf4/

      它会渲染所有语言内容;您应该考虑在 php 代码中进行语言选择以避免额外的流量,除非您只想通过 javascript 切换语言而无需任何服务器交互。

      问候一月

      也如 Arun P Johny 所说

      ...您的脚本未添加到 dom 就绪处理程序中...

      问候一月

      【讨论】:

      • 感谢您的建议,但在我有时间实际检查之前 - 在您的代码中,我在 index.php 中包含语言 php 文件,但我需要在检查语言的文件中访问它们。在索引文件中,我设法使用 $_GET 方法导入了正确的文件。
      猜你喜欢
      • 2011-10-13
      • 2015-05-17
      • 2018-01-27
      • 2017-11-28
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      相关资源
      最近更新 更多