【问题标题】:javascript wont parse php html tagsjavascript不会解析php html标签
【发布时间】:2009-09-11 12:42:22
【问题描述】:

php通过包裹在<p class="select"></p>标签中的ajax将html字符串发送到html,css完美读取类。 javascript/jquery 没有。 javascript/jquery 甚至不会解析<p onclick="function()"></p>。我做错了什么?

这是我的 php(通过 ajax 发送数据很好)

echo "<p class='select' onclick='populate();' >{$row['song_name']} </p>";

这是我的 CSS(工作正常)

p.select{color:#fff;padding: 5px;margin:0}
p.select:hover{color:#fff;background:#f60;cursor:pointer;}

这是我的 javascript 方法1 jquery。

$("p .select").click(function(){
        var str = $(this).val();
        alert(str);
  });

方法2 onclick 函数。

function populate()
{

alert('here')
}

这两种方法都没有响应。大师这是为什么?

【问题讨论】:

    标签: php javascript jquery ajax


    【解决方案1】:
    $("p .select").live ( "click" , function(){
            var str = $(this).text();
            alert(str);
      });
    

    Events/live

    将处理程序绑定到事件(如 点击)为所有当前 - 和未来 - 匹配的元素。

    【讨论】:

    【解决方案2】:

    两件事:

    • p .select 将选择&lt;p&gt; 标签包含 具有类选择的元素。 p.select 选择 &lt;p class="select"&gt;
    • 为什么不将populate 函数移到直播中?我怀疑 jquery live(或 click)会删除任何显式处理程序。

    试试这个:

    $("p.select").live("click",function()
    {
        var str = $(this).text();
        alert(str);
        populate();
    });
    

    【讨论】:

      【解决方案3】:

      我在下面发布了一个工作(在 Firefox 中)示例。我认为您忘记将 jquery 方法放在 onload 事件中。除了其他(小)错误...

      <html>
         <head>
            <style>
               p.select{color:#fff;padding: 5px;margin:0}
               p.select:hover{color:#fff;background:#f60;cursor:pointer;}
            </style>      
            <script src="jquery-1.3.2.min(2).js" type="text/javascript"></script>
            <script type="text/javascript">         
               function bodyOnLoad() {
                  //instead of "p .select" "p.select"
                  $("p.select").click(
                     function(){
                        //changed val() into text()
                        var str = $(this).text();
                        alert(str);
                     }); 
               }
            </script>         
         </head>
         <body onload="bodyOnLoad()">
            <p class='select'>Songname</p>
         </body>
      </html>
      

      【讨论】:

      • jQuery 在$(document).ready(function(){...}); 中有一个比&lt;body onload="bodyOnLoad()&gt;" 更简单的构建
      猜你喜欢
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多