有没有办法让按钮重定向到当前 link 的页面,但使用另一种语言
Is there a way to make a button redirect to the page of the current link, but in another language
您好,我有 2 个博客,使用两种不同的语言。所有 link 都是相同的,除了英文博客在博客名称后有一个“-en”后缀,而希腊博客有一个“-el”后缀。
我想在使用当前页面 link 的某处放置一个按钮。例如“https://cookwithnick-en.blogspot.com/2021/07/mini-piroshki.html”,将其转换为“https://cookwithnick-el.blogspot.com/2021/07/mini-piroshki。 html" 并在同一选项卡上打开它。
我已成功编写了以下代码,但无法正常工作:
<input type="button" onclick="location.href=window.location.href.replace("en", "el");" value="Greek" />
如果“.replace(”en”, “el”);”代码有效丢失(它重定向到完全相同的页面)我想将 link 转换为其他语言。
感谢您的宝贵时间和建议。
您需要对字符串使用单引号,因为 onclick
属性的值包含在双引号中。
<input type="button" onclick="location.href=window.location.href.replace('en', 'el');" value="Greek" />
但是,通常最好使用 addEventListener
而不是内联事件处理程序。
只需将 .replace("en", "el")
中的双引号替换为单引号即可 所以:
<input type="button" onclick="location.href=window.location.href.replace('en', 'el');" value="Greek" />
不过,最好这样做:
<input type="button" id="change-lang" value="Greek" />
<script>
document.getElementById("change-lang").addEventListener("click", function(){
location.href=window.location.href.replace('en', 'el');
});
</script>
您好,我有 2 个博客,使用两种不同的语言。所有 link 都是相同的,除了英文博客在博客名称后有一个“-en”后缀,而希腊博客有一个“-el”后缀。
我想在使用当前页面 link 的某处放置一个按钮。例如“https://cookwithnick-en.blogspot.com/2021/07/mini-piroshki.html”,将其转换为“https://cookwithnick-el.blogspot.com/2021/07/mini-piroshki。 html" 并在同一选项卡上打开它。
我已成功编写了以下代码,但无法正常工作:
<input type="button" onclick="location.href=window.location.href.replace("en", "el");" value="Greek" />
如果“.replace(”en”, “el”);”代码有效丢失(它重定向到完全相同的页面)我想将 link 转换为其他语言。
感谢您的宝贵时间和建议。
您需要对字符串使用单引号,因为 onclick
属性的值包含在双引号中。
<input type="button" onclick="location.href=window.location.href.replace('en', 'el');" value="Greek" />
但是,通常最好使用 addEventListener
而不是内联事件处理程序。
只需将 .replace("en", "el")
中的双引号替换为单引号即可 所以:
<input type="button" onclick="location.href=window.location.href.replace('en', 'el');" value="Greek" />
不过,最好这样做:
<input type="button" id="change-lang" value="Greek" />
<script>
document.getElementById("change-lang").addEventListener("click", function(){
location.href=window.location.href.replace('en', 'el');
});
</script>