如何更改同一页面上的语言文件夹? (通过使用 jQuery 或 JavaScript)
我有一个包含 2 种语言的网站,即 En
和 Th
。每种语言在不同的文件夹中具有相同的文件名,即/en/
和/th/
。我在包含的头文件 (asp) 上创建了 标记来在这两种语言之间切换。
问题是无论你在哪里,如何获取当前路径,并将文件夹名称更改为en
或th
? (如果您在英语联系页面中,单击th
将带您进入泰语联系页面。)
我尝试创建一个函数:
<script>
function changeLanguage(to) {
var from = jQuery.url.segment(-2);
var url = from.replace('/' + from + '/', to);
document.location = url;
}
</script>
<a id="flags" href="#" onclick="changeLanguage(en)"> English </a>|<a href="#" onclick="changeLanguage(th)" >Thai</a>
但这不起作用。我花了很多天才找到类似的问题。我找到了一些,但仍然无法应用于我的代码。所以请帮助我。我在这方面真的是新手,真的需要你的帮助。非常感谢。
I have a site with 2 languages that are En
and Th
. Each language has the same file name in different folder, i.e. /en/
and /th/
. I've created <a>
tag to switch between these 2 languages on my included header file (asp).
The question is how to get the current path wherever you are, and change folder name to en
or th
? (If you are inside English Contact page, clicking th
will take you to Thai Contact page.)
I have tried to create a function:
<script>
function changeLanguage(to) {
var from = jQuery.url.segment(-2);
var url = from.replace('/' + from + '/', to);
document.location = url;
}
</script>
to use in <a>
tag.
<a id="flags" href="#" onclick="changeLanguage(en)"> English </a>|<a href="#" onclick="changeLanguage(th)" >Thai</a>
But it does not work. I've spent so many days to find a similar question. I found some but still couldn't apply to my code. So please help me. I'm really newbie on this and really need your help. Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加一个字符串,此时您正在说changeLanguage(名为en的变量),使用:
或使用jquery为您绑定点击事件:
并使用jquery代码:
我已将语言文本移至href,所以我们可以在点击事件中捕获它。
一个示例,将弹出有关您单击的语言的警报: http://jsbin.com/epavoh/2/
you need to add a string, at the moment you are saying changeLanguage(variable named en), use:
or use jquery to bind the click event for you:
and use the jquery code:
I have moved the language text to the href instead so we can catch that in the click event.
an example that will pop an alert to what language you clicked on: http://jsbin.com/epavoh/2/
@voigtan,我花了时间尝试理解你的代码。但无论我尝试过,此代码仍然在我的 URL 之后添加 #en 或 #th。
是
的,我知道 jQuery.url.segment(-2) 的用途。它会从右边的 2 个路径中找到 URL 段,对吗?我的文件夹是 root/en/default.asp,因此 (-2) 将返回“en”或“th”的值。
实际上,不需要是 Javascript 或 jQuery。任何东西只要能适合我的 .asp 就可以了,仅此而已。
@voigtan, I've spent time try to understand your code. But no matter i've tried, this code still add #en or #th after my URL.
with
Yes, I know what jQuery.url.segment(-2) use for. It will find the segment of URL from 2 path from the right, right? my folder was root/en/default.asp, so (-2) will return the value of "en" or "th".
Actually, no need to be Javascript or jQuery. Any thing would be ok just can fit my .asp, that's all.