jQuery:将字符串添加到 URL

发布于 2024-11-27 22:48:32 字数 495 浏览 1 评论 0原文

我正在使用这段代码从单击的链接中获取 url:

var url = $(this).find('a').attr('href');

我想向该 url 添加一个字符串。

例如,我当前的网址是:

http://mydomain.com/myarticle

我想将其设置为:

http://mydomain.com/myarticle-chinese

我应该在我的第一行代码中添加什么来实现这一点?

我将不胜感激您的建议!

附录:非常感谢!四个人几乎同时回答,四个人都给了我有用的答案。我希望我能接受全部四个!

I'm using this snippet of code to grab a url from a clicked link:

var url = $(this).find('a').attr('href');

I want to add a string to that url.

For example, my current url is:

http://mydomain.com/myarticle

I want to make it:

http://mydomain.com/myarticle-chinese

What should I add to my initial line of code to make that happen?

I'd be grateful for your advice!

ADDENDUM: THANK YOU VERY MUCH! FOUR PEOPLE ANSWERED ALMOST SIMULTANEOUSLY AND ALL FOUR GAVE ME A HELPFUL ANSWER. I WISH I COULD ACCEPT ALL FOUR!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

双手揣兜 2024-12-04 22:48:32
var $a = $(this).find('a');
var url = $a.attr('href');
$a.attr('href', url + '-chinese');
var $a = $(this).find('a');
var url = $a.attr('href');
$a.attr('href', url + '-chinese');
深海夜未眠 2024-12-04 22:48:32
var url = $(this).find('a').attr('href') + "-chinese";
var url = $(this).find('a').attr('href') + "-chinese";
原来分手还会想你 2024-12-04 22:48:32

这会将新值存储在 url: 中,

var url = $(this).find('a').attr('href') + '-chinese';

并且这将重定向用户的浏览器:

window.location.href = $(this).find('a').attr('href') + '-chinese'

This will store the new value in url:

var url = $(this).find('a').attr('href') + '-chinese';

and this will redirect the user's browser:

window.location.href = $(this).find('a').attr('href') + '-chinese'
守护在此方 2024-12-04 22:48:32

您想将字符串“-chinese”附加到检索到的 URL 中吗?试试这个:

var url = $(this).find('a').attr('href') + '-chinese';

You want to append the string "-chinese" to the retrieved URL? Try this:

var url = $(this).find('a').attr('href') + '-chinese';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文