简体   繁体   English

引用 javascript(jquery)-html-php

[英]quotes javascript(jquery)-html-php

I'm stack in such question:我对这样的问题很感兴趣:

example例子

<td>
    <span style="cursor:pointer;" onclick="$(this).parent().html('<input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),'title' ,'3')}\'>');">
        <?=$result['settings_title']?>
    </span>
</td>

looks like fine but in browser I get "Uncaught SyntaxError: Unexpected identifier" in this row(chrome listing):看起来不错,但在浏览器中,我在此行中收到“未捕获的语法错误:意外的标识符”(chrome 列表):

<span style="cursor:pointer;" onclick="$(this).parent().html('input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),'title' ,'3')}\'>');">asdasd</span>

Please, help me - if somebody had such trouble before.Where am I wrong?请帮助我 - 如果有人以前遇到过这样的麻烦。我错在哪里? Maybe will be better do it via PHP echo(I guess not:) )?也许通过 PHP 回声会更好(我猜不是:))?

I think you are still missing some escapes in the send_ajax function, try this:我认为您仍然缺少 send_ajax function 中的一些转义,试试这个:

<span style="cursor:pointer;" onclick="$(this).parent().html('input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),\'title\' ,\'3\')}\'>');">asdasd</span>

Alternatively to keep things more manageable, you can make the onclick call a function that executes the given code.或者,为了让事情更易于管理,您可以让 onclick 调用执行给定代码的 function。 This means you don't have to worry about escaping so much and should be a lot easier for you.这意味着您不必担心 escaping 对您来说应该容易得多。

You didn't escape apostrophes in 'this' and '3'.您没有在“this”和“3”中转义撇号。
The right markup is this:正确的标记是这样的:

<span style="cursor:pointer;" onclick="$(this).parent().html('input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),\'title\' ,\'3\')}\'>');">asdasd</span>

However, this kind of binding events is a very bad practice, and conduces to the type of mistakes you've just had.但是,这种绑定事件是一种非常糟糕的做法,并且会导致您刚刚遇到的错误类型。 You should do it like this:你应该这样做:

 <span style="cursor:pointer;" class="spanClass" data-param1="title" data-param2="3">asdasd</span>
 <span style="cursor:pointer;" class="spanClass" data-param1="title2" data-param2="5">asdasd</span>
 ... more spans generated dynamically

and have this:并有这个:

   $(document).ready(function(){
     $(".spanClass").click(function(){
          var param1 = $(this).attr("data-param1");
          var param2 = $(this).attr("data-param2");
          $('<input type="text" value="asdasd" />').keydown(function(event){
            if(char_click(event)==13) {
               send_ajax_settings($(this), param1 , param2));
            }
          }).appendTo($(this).parent());
      });
    });

So, you can have javascript and html separated, and will work for any amount of elements generated dynamically by the server.因此,您可以将 javascript 和 html 分开,并且适用于服务器动态生成的任意数量的元素。

Hope this helps.希望这可以帮助。 Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM