jQuery: Add a String to URL

孤街浪徒 提交于 2019-12-24 06:04:24

问题


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!


回答1:


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



回答2:


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



回答3:


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'



回答4:


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

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


来源:https://stackoverflow.com/questions/6934711/jquery-add-a-string-to-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!