javascript 如何在 jquery 中注释带有 php 代码行的 jquery 代码块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9495405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:51:44  来源:igfitidea点击:

how to comment a jquery code block which has a line with php code in jquery

phpjavascriptjqueryajax

提问by Kanishka Panamaldeniya

i want to comment a code block, but i have a line in my javascript block which has jquery and php both

我想评论一个代码块,但我的 javascript 块中有一行包含 jquery 和 php

like this

像这样

jq('.time_slot').each(function(index) {
    var a = jq(this).autocomplete({ 
        serviceUrl:"<? echo $this->config->item('base_url'); ?>business/information/add/autocomplete",
        params: { suggessions_id:28767 }, //aditional parameters
        onSelect:function(value,data){ jq(this).trigger('change');  } 
    });
});

with out this line

没有这条线

      serviceUrl:"<? echo $this->config->item('base_url'); ?>business/information/add/autocomplete",

i can comment this block using

我可以使用

/*

*/

but now i can not use it , also i tried with single line comments , like this

但现在我不能使用它,我也试过单行注释,像这样

//jq('.time_slot').each(function(index) {
    //var a = jq(this).autocomplete({ 
        //serviceUrl:"<? echo $this->config->item('base_url'); ?>business/information/add/autocomplete",
       // params: { suggessions_id:28767 }, //aditional parameters
       // onSelect:function(value,data){ jq(this).trigger('change');  } 
   // });
   // });

but for this line

但对于这条线

 serviceUrl:"<? echo $this->config->item('base_url'); ?>business/information/add/autocomplete",

it is not working .

它不工作。

so i ended up with a very odinary solution like this

所以我最终得到了一个非常普通的解决方案

 //serviceUrl:"
<? //echo $this->config->item('base_url'); ?>
//business/information/add/autocomplete",

what is the best way to comment my code block , thank you in advance .

评论我的代码块的最佳方式是什么,提前致谢。

回答by Andreas Wong

On runtime, if your php configuration's short_open_tag is enabled, those script enclosed in <? ?>would have been parsed by php parser in the server before sending the html (that contains the said javascript with comment) to the browser.

在运行时,如果您的 php 配置的 short_open_tag 已启用,则在<? ?>将 html(包含上述带有注释的 javascript)发送到浏览器之前,服务器中的 php 解析器会解析包含在其中的那些脚本。

So it should technically work if you use // to comment out that line, as the javascript parser won't be aware that the string was generated from php.

因此,如果您使用 // 注释掉该行,它在技术上应该有效,因为 javascript 解析器不会知道该字符串是从 php 生成的。

Make sure you have short_open_tag enabled or use <?phpinstead of short tags (<?).

确保您已启用 short_open_tag 或使用<?php短标签代替(<?)

回答by Aaron

Ok, admittedly, this isn't pretty, but if you want to cancel out the commingled PHP and JavaScript in one fell swoop, you might consider wrapping the entire block in a PHP condition that always returns false, e.g:

好吧,诚然,这并不漂亮,但是如果您想一举取消混合的 PHP 和 JavaScript,您可以考虑将整个块包装在始终返回 false 的 PHP 条件中,例如:

<?php if (1 == 0) { ?>
jq('.time_slot').each(function(index) {
    var a = jq(this).autocomplete({ 
        serviceUrl:"<? echo $this->config->item('base_url'); ?>business/information/add/autocomplete",
        params: { suggessions_id:28767 }, //aditional parameters
        onSelect:function(value,data){ jq(this).trigger('change');  } 
    });
});
<?php } ?>

Ugly, I know, but undeniably quick and effective for big blocks of mixed code.

丑陋,我知道,但不可否认,对于大块的混合代码来说是快速有效的。

回答by Tomasz Banasiak

Afaik theres no better solution that you provided. PHP and JS comments are always separate, you can put JS comment into PHP's echo function.

Afaik 没有您提供的更好的解决方案。PHP 和 JS 注释总是分开的,你可以将 JS 注释放入 PHP 的 echo 函数中。

PHP parser will always try to run code between blocks.

PHP 解析器将始终尝试在块之间运行代码。

Imho best solution is to comment whole JS code using /* and */ and use single line comment to prevent PHP running php code block.

恕我直言,最好的解决方案是使用 /* 和 */ 注释整个 JS 代码,并使用单行注释来防止 PHP 运行 php 代码块。

回答by prukuhkoo

That is the best way already. Can't think of any better ways..

这已经是最好的办法了。想不出更好的办法了。。