2012-02-29 57 views
0

PHP代碼線我要評論一個代碼塊,但我已經在我的JavaScript塊中的線,有jQuery和PHP都怎麼評論一個jQuery代碼塊,具有與jQuery的

這樣

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'); } 
    }); 
}); 

與出這個線

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

我可以評論使用該塊

/* 

*/ 

,但現在我不能使用它,我也試圖與單行註釋,這樣

//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'); } 
    // }); 
    // }); 

但對於此行

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

它無法正常工作。

,所以我結束了這樣的

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

什麼是評論我的代碼塊的最佳方式非常odinary解決方案,預先感謝您。

回答

1

好的,不可否認,這不是很好,但如果你想一舉取消混合的PHP和JavaScript,你可能會考慮將整個塊包裹在一個PHP條件始終返回false,例如:

<?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 } ?> 

醜陋,我知道,但不可否認的快速和有效的混合代碼大塊。

+2

非常非常難看............:D:D – 2012-02-29 08:10:15

2

在運行時,如果您的php配置的short_open_tag已啓用,那麼在將包含<? ?>的腳本發送給瀏覽器之前,服務器中的php分析器將解析這些腳本。

因此,如果您使用//註釋掉該行,它應該在技術上工作,因爲javascript解析器不會意識到該字符串是從php生成的。

確保您啓用了short_open_tag,或使用<?php而不是短標籤(<?)

0

Afaik theres沒有您提供的更好的解決方案。 PHP和JS註釋總是分開的,您可以將JS註釋放入PHP的回顯函數中。

PHP解析器將總是嘗試在塊之間運行代碼。

Imho最好的解決方案是使用/ *和* /評論整個JS代碼,並使用單行註釋來防止PHP運行php代碼塊。

0

這是最好的方法了。想不到更好的辦法..