Netbeans PHP/javascript highlighting

笑着哭i 提交于 2019-12-10 07:11:18

问题


I've been searching all over for a solution to faulty errors in the code highlighting for NetBeans (7.1.1) when mixing PHP with Javascript. I also use Notepad++ which has no problems with this.

Example:

function showUpload<?php echo $upload;?>(file) { /* JS-code */ }

or:

$('.option-help').qtip({
    content: function(api) { return $(this).parent().attr('data-tip'); },
<?php if ($help == 'icon') { ?>
    show: { event: 'click' },
<?php } ?>
    position: { my: 'bottom left', at: 'top left', of: $(this) }
});

I did find some work-arounds, but I refuse to change code for my IDE!!! (The IDE should help me code, not make things more difficult)

Does anyone know of an actual solution to this problem?


回答1:


If you're using only JavaScript + PHP combinations in your file (without HTML), just open the file in which you want to highlight JavaScript syntax, and add this line:

//<script type="text/javascript">

on the top of the page before opening the first PHP tag like this:

//<script type="text/javascript">
<?php
/*The rest of the code..*/
?>

All JavaScript code in this file should be properly highlighted.

In case that you have HTML code somewhere in the middle of the file, you would need to close JavaScript tag before HTML code, and to re-open it again after HTML code like this:

//<script type="text/javascript">
<?php
/*The rest of the code..JavaScript + PHP*/

//</script>
<h1>HTML code</h1><!--HTML code only-->
<p>Bla bla..</p>
//<script type="text/javascript">

/*JavaScript + PHP code again*/

?>

If you don't want these comments to be printed out to your final HTML document, you can create fake PHP function that will never be used by you, only by IDE, on the top of the document like this:

<?php
function higlightJavaScriptCode(){
    ?>
    //<script type="text/javascript">
    <?php
}
/*The rest of the code..*/
?>



回答2:


@gradosevic Nailed it above, I'll just add my own ultra-compact version that won't output anything to browser:

// Fix Netbeans' jQuery highlighting in mixed PHP/JS. See https://stackoverflow.com/a/21440810/209859
function higlightJavaScriptCode(){?>//<script type="text/javascript"><?php }

I guess the only problem is you can only have one of these per project, or you'd have to start namespacing them.



来源:https://stackoverflow.com/questions/10161818/netbeans-php-javascript-highlighting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!