0

我有一个问题,我通过 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 文件。但也许原因是因为这个脚本在带有标题的 php 文件中,<?php header('Content-type: text/javascript'); ?>我在索引页面中附加了像 javascript 文件这样的文件,比如<script type="text/javascript" src="php/FormuValidacija.php" /></script>

我试过了

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

但效果不佳:(

PS对不起,如果这是个愚蠢的问题,我是新手,但愿意学习!:)

4

4 回答 4

2

看起来您的脚本未添加到 dom 就绪处理程序中,例如

jQuery(function($){
alert($('#checkLang').length)//it should alert 1 not 0
if ($('#checkLang').hasClass('RU')){
    <?php include("lan/ru.php"); ?>
};
})
于 2013-08-22T06:15:26.900 回答
1

当前语言:

<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)

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

于 2013-08-22T06:53:02.790 回答
0

Given my jsfiddle example here, you can clearly see that javascript is able to check that the given class is set on the div.

http://jsfiddle.net/n5e9a/

Both of the php files will be rendered no matter what, because the if check here is done on the client side javascript. if you wish to only render data from the php files once the check has been done on the client side. Then you have to fetch the data from the client side instead of serving it from the server side.

One way of doing this is through ajax. for example. you could create a php script that returns html contents based on a query, something like:

checklang.php?lang=RU

and in the javascript code you would have a request set up like this:

$.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);
});
于 2013-08-22T06:25:26.927 回答
0

它包含您的内容,因为它已经生成了服务器端,因此当 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 就绪处理程序中...

问候简

于 2013-08-22T06:27:35.907 回答