简体   繁体   English

如何在HTML的两个标签之间编写javascript?

[英]How can I can write javascript between two tags in HTML?

I have a PHP file that inserts code into an HTML5 file. 我有一个将文件插入HTML5文件的PHP文件。 Something like this: 像这样:

// this works fine
echo '<li>...something</li>';
// this doesn't work
echo '<script type="text/javascript">document.write(\'<li>...something...</li>\');</script>';

If I "echo" these lines to the screen they both work OK (Of course, this is plain code!). 如果我将这些行“回显”到屏幕上,它们都可以正常工作(当然,这是纯代码!)。 BUT this code above is "echo":ed into an HTML5 file and executed THERE. 但是上面的代码是“回显”的:被编码为HTML5文件并在那里执行。 The first option works, while the second one doesn't (producing sometimes error 1561). 第一个选项有效,而第二个则无效(有时会产生错误1561)。

Example: http://www.w3schools.com/PHP/php_ajax_database.asp 示例: http//www.w3schools.com/PHP/php_ajax_database.asp

Another example: http://www.modilo.net/dump.html 另一个示例: http : //www.modilo.net/dump.html

Can anyone tell me how I can wrie javascript between two tags in HTML code?? 谁能告诉我如何在HTML代码中的两个标记之间使用javascript?

The Javascript BOO isn't set, so you need to either set it in your echo like: 未设置Javascript BOO,因此您需要在回显中设置它,例如:

echo '<script type="text/javascript"> var BOO = '. $BOO .'; // blah</script>';

Or, just put the value in, so it gets printed out to the user's browser: 或者,只需输入该值,即可将其输出到用户的浏览器中:

... if( '. $BOO .' ) ...

For Javascript to work, write down it in the appropriated tags. 为了使JavaScript正常运行,请在相应的标签中写下来。 Try the code below. 试试下面的代码。

<html>
<head>
<?php // dump


// Lets say $BOO in PHP and BOO in javascript have the same value.
$BOO = true;
// Option 1 works fine:
if ( $BOO ) { echo '<li> PHP ... something ... </li>'; } else echo '<li> PHP ...     something else ... </li>';

// Option 2 doesn't work (and I need this!):
echo '<script type="text/javascript">
var BOO = '.$BOO.';
if (BOO) 
document.write(\'<li> JS... something ... </li>\') 
else 
document.write(\'<li> JS ... something else ... </li>\');';
echo '</script>';
?>
</head>
</html>

This code has the optput 该代码具有optput

PHP ... something ... PHP ...一些...

JS... something ... JS ...某事...

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

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