【问题标题】:Unable to target div with JS无法使用 JS 定位 div
【发布时间】:2012-05-29 05:22:39
【问题描述】:

我正在尝试使用 js 定位嵌套在某些 php 中的 div,但无论我做什么,我似乎都无法定位它。这是供参考的代码: php:

echo "<li>"."<div id=\"countryclick\">". "<a href=\"?Continent=$Continent&Country=$Country\">" . $Country . " ". "</a>" . "</div>"."</li>";

js:

$("#countryclick").click(function(){
    $("#country").hide();
    $("#city").show();
});

有什么想法吗? 提前致谢!

【问题讨论】:

  • 尝试将你的 js 嵌套在 $(document).ready();
  • 实际上有一些我没有包含的 js 有 $(document).ready();

标签: php javascript html target capture


【解决方案1】:
div#countryclick 

是动态创建的,因此您可以使用 jQuery 的 .live。 喜欢:

$("#countryclick").live("click",function(){
   //code here
})

【讨论】:

  • Zhujy 就在这里,如果您在运行时创建 div,这可能是您的答案。
【解决方案2】:

我不确定您是否正在动态创建 div。如果您是动态创建它,则必须使用 live 方法,如下所示:

$("#countryclick").live("click",function(){
    $("#country").hide();
    $("#city").show();
});

请检查一下。

【讨论】:

    【解决方案3】:
    <li>
        <div id="countryclick">
          <a href="?Continent=<?php echo $Continent; ?>&Country=<?php echo $Country; ?>">
            LINK HERE
          </a>
        </div>
    </li>
    <div id="country">country</div>
    <div id="city">city</div>
    
    $("div#countryclick").live("click",function(){
        $("#country").hide();
        $("#city").show();
    });
    

    这应该可以工作。

    【讨论】:

      【解决方案4】:

      从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。如果你使用一些旧版本的 jQuery,你应该使用 .delegate() 而不是 .live()。

      Visit here live()Here on()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多