开发者

Changing src value through jQuery

开发者 https://www.devze.com 2023-03-13 07:05 出处:网络
I have a jQuery variable merImg like this var merImg = \'<a href=\"http://xxx.com\"><img src=\"http://om.com/pion/thumbnail/11x65/aa/img.jpg\" border=\"0\" align=\"left\" h开发者_StackOverfl

I have a jQuery variable merImg like this

var merImg = '<a href="http://xxx.com"><img src="http://om.com/pion/thumbnail/11x65/aa/img.jpg" border="0" align="left" h开发者_StackOverfloweight="11" width="65"></a>';

I want to replace thumbnail/11x65 in the src with image/40x using jQuery. Is there a regex to do this easier? Or any logic at all to change it?

Also I want to remove the height and width attribute to the img tag. How do i go about it?


You could use the standard replace() method of javascript:

merImg = merImg.replace('thumbnail/11x65', 'image/40x');

as the first argument you can also pass a regExp like this

   merImg = merImg.replace(/your regexp/, 'image/40x');

To remove height and width in this case you could replace them with an empty string:

merImg = merImg.replace('width="65"', '');
merImg = merImg.replace('height="11"', '');

I'm no experto of regular experssions but you can write a general regular expression to strip away width and height attributes from a string.

Also a llot of people suggest (correctly) that you should use an HTML parser to parse HTMl. Try to google it if you need more info about it

But in this particulare what i've written should work

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号