dealing with javascript enclosed in a php string
so in such situation you lose the javascript hi开发者_JS百科ghlight syntax, to me it looks like there are some cases were youre obliged to put the Js in a php string (for example when working on some ajax stuff and php mysql request), how you do? i found very annoying to work js this way
echo '<script>
$(function(){
$("select#discipline_").change(function(){
var selected = $("select#discipline_ option:selected");
$.ajax({
url: "valider.php?discipline_val="+selected.val()+"&action=specialite",
success: function(response){
$("span#specialite").html(response)
if( selected.val()=="0" ){
$("select#specialite_").hide()
$("span#add").hide()
}
else{
$("select#specialite_").css({width:"0px"}).animate({width:"204px"}, 300).fadeIn()
$("#add").show()
} } } ) } )
})</script>';
Generally, it is really preferable to keep large blocks of JavaScript in a separate file.
This also forces one to keep a tidy structure in the JavaScript - splitting things into functions and classes - which is a good thing.
If you are in the PHP body, you can easily break out to HTML:
... PHP code ....
?>
<script>
... Javascript code
</script>
<?php
... PHP code ....
<?php if($a==1){ ?>
<div id="helloDiv">hi</div>
<?php } ?>
Like Luis mentioned, that's the best way to do it unless you want to ad \ in front of all ' and "
精彩评论