PHP 页面中的 JavaScript 函数导致 PHP 选项卡格式出现问题
我试图在 PHP 文件中引用包含表排序函数的 JavaScript 文件。 一切都按其应有的方式运行,但是一旦我将下面的代码添加到我的 PHP 报告页面,它就会导致我的导航页面上的选项卡格式出现问题。如果我在 PHP 代码之外添加随机 document.write
,也会出现同样的问题。这不是一个大问题,但对用户来说相当难看。
有没有更好的方法来引用 JavaScript 源文档,并且不会干扰我的 php 导航页面格式?我觉得有一个简单的修复方法我没有掌握。
<script language="JavaScript" src="include/sorttable.js"></script>
还使用 PHP 标签内的 PHP echo
进行调用,同样的问题。
以下是出现格式问题的选项卡的导航代码的一部分。
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////
function start_page_printer_friendly(
$section = "home",
$headContents = null,
$bodyAttributes = null
)
//////////////////////////////////////////////////////////////////////////////////////////////////
{?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php print $page_title; ?></title>
<link href="/trellis_portal/include/portal.css" media="screen"
rel="Stylesheet" type="text/css" />
<?php if ( ! is_null($headContents)) { print $headContents; } ?>
</head>
<body onload="printpage();" <?php if ( ! is_null($bodyAttributes)) { print $bodyAttributes; } ?>>
<script language="JavaScript">
<!--
function printpage() { window.print(); }
-->
</script>
<br />
<?php
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function start_page( $section = "home",
$subsection = null,
$headContents = null,
$bodyAttributes = null
)
//////////////////////////////////////////////////////////////////////////////////////////////////
{
if (isset($_GET['printer']))
{
start_page_printer_friendly();
return;
}
$section = strtolower($section);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php print $page_title; ?></title>
<?php if ( ! is_null($headContents)) { print $headContents; } ?>
</head>
<body class=" yui-skin-sam" <?php if ( ! is_null($bodyAttributes)) { print $bodyAttributes; } ?>>
<br />
<div id="header">
<ul>
<!--
<li><a class="<?php print tabSelector($section, 'executive'); ?>"
href="/trellis_portal/portal.php?section=executive&title=Executive">Executive</a></li>
<li><a
class="<?php print tabSelector($section, 'community'); ?>"
href="/trellis_portal/portal.php?section=community&title=Community">Community</a></li>
-->
<li><a class="<?php print tabSelector($section, 'trellis'); ?>"
href="/trellis_portal/portal.php?section=trellis&title=Trellis">Trellis</a></li>
<!--
I am trying to reference a JavaScript file, containing a table sort function, in a PHP file.
Everything functions the way it should, but once I add the code below to my PHP report page it causes issues with the format of the tabs on my navigation page. The same issue comes up if I add a random document.write
outside of the PHP code. Not a huge issue but rather unsightly to the user.
Is there a better way to reference the JavaScript source doc that will not interfer with my php navigation page format? I feel like there is a simple fix that I am not grasping.
<script language="JavaScript" src="include/sorttable.js"></script>
Also used an the PHP echo
inside the PHP tags to make the call, same issue.
Here is part of the navigation code for the tabs that the format issue is coming up on.
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////
function start_page_printer_friendly(
$section = "home",
$headContents = null,
$bodyAttributes = null
)
//////////////////////////////////////////////////////////////////////////////////////////////////
{?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php print $page_title; ?></title>
<link href="/trellis_portal/include/portal.css" media="screen"
rel="Stylesheet" type="text/css" />
<?php if ( ! is_null($headContents)) { print $headContents; } ?>
</head>
<body onload="printpage();" <?php if ( ! is_null($bodyAttributes)) { print $bodyAttributes; } ?>>
<script language="JavaScript">
<!--
function printpage() { window.print(); }
-->
</script>
<br />
<?php
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function start_page( $section = "home",
$subsection = null,
$headContents = null,
$bodyAttributes = null
)
//////////////////////////////////////////////////////////////////////////////////////////////////
{
if (isset($_GET['printer']))
{
start_page_printer_friendly();
return;
}
$section = strtolower($section);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php print $page_title; ?></title>
<?php if ( ! is_null($headContents)) { print $headContents; } ?>
</head>
<body class=" yui-skin-sam" <?php if ( ! is_null($bodyAttributes)) { print $bodyAttributes; } ?>>
<br />
<div id="header">
<ul>
<!--
<li><a class="<?php print tabSelector($section, 'executive'); ?>"
href="/trellis_portal/portal.php?section=executive&title=Executive">Executive</a></li>
<li><a
class="<?php print tabSelector($section, 'community'); ?>"
href="/trellis_portal/portal.php?section=community&title=Community">Community</a></li>
-->
<li><a class="<?php print tabSelector($section, 'trellis'); ?>"
href="/trellis_portal/portal.php?section=trellis&title=Trellis">Trellis</a></li>
<!--
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我仍然不明白你的期望,但我已经看到一件事:外面的一切都被字面转置,因此
会在 DOCTYPE 之前的开头引入一个额外的空格(换行符)。
另外脚本的结尾( --> )也会产生 JS 语法错误。它是有效的 HTML,但必须在 JS 中注释掉 ( //--> )。
I still don't get what you expect but one thing I can see already: EVERYTHING outside is literally transposed, thus
will introduce an extra whitespace (newline) at the start before DOCTYPE.
Also the ending of the script ( --> ) will generate JS syntax error. It is valid HTML but must be commented out ( //--> ) in JS.
我还不太了解情况,但您需要注意,您的页面上没有任何与 PHP 相关的内容,只有 PHP 生成的 HTML/CSS/Javascript 代码。因此,您需要查看 HTML 代码并查看问题所在。然后您可能想用实际的 HTML 代码更新您的问题。
I don't really understand the situation yet, but you need to be aware that there is nothing PHP related on your page, only HTML/CSS/Javascript code that happened to be generated by PHP. So you need to look into your HTML code and see what the problem is there. You may then want to update your question with actual HTML code.
SCRIPT 标签本身不能影响页面内的元素或布局。
您的问题可能出在其他地方。为了调试这个,我会忽略 PHP 并专注于 HTML 输出并查找未封闭的标签和无效的标记等。
SCRIPT tags, in themselves, cannot affect elements or layout within the page.
Your problem is likely elsewhere. To debug this I would ignore the PHP and focus on the HTML output and look for unclosed tags and invalid markup etc.