我有一个小问题,我有以下代码,想向我的网站添加语言切换。假设我有header.php和index.php。我想在header.php中包含按钮和脚本。Header.php包含在index.php中。如何从index.php访问标头中的函数?
我的代码:
在Header.php中
<head>
<style type="text/css">
body.fr > p[lang=en] {
display: none;
}
body.en > p[lang=fr] {
display: none;
}
</style>
</head>
<button onclick="document.body.className='en'">English</button>
<button onclick="document.body.className='fr'">French</button>
在Index.php中
<?php
include('header.php');
?>
<body class="en">
<p lang="en">This is English</p>
<p lang="fr">This is French</p>
</body>
如果我单击按钮,则文本不会更改。我假设我必须将header.php与index.php内容集成在一起,但是又如何呢?
我不确定为什么在header.php和index.php文件中都具有按钮,但是>
从CSS中删除似乎可以解决问题。我也建议您格式化代码,类似于以下内容:
header.php
<head>
<title>Languages</titke>
<!-- Meta tags CSS and JavaScript Files -->
</head>
language_buttons.php
<button onclick="document.body.className='en'">English</button>
<button onclick="document.body.className='fr'">French</button>
footer.php
<style type="text/css">
body.fr p[lang=en] {
display: none;
}
body.en p[lang=fr] {
display: none;
}
</style>
index.php
<!DOCTYPE html>
<html>
<?php include('header.php'); ?>
<body class="en">
<?php include 'language_buttons.php'; ?>
<p lang="en">This is English</p>
<p lang="fr">This is French</p>
<?php include 'footer.php'; ?>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句