jquery attr 设定src中含有&(宏)符号问题的解决方法


问题描述:

html代码如下:

<iframe src="http://www.phpstudy.net" id="addOrUpdateIframe" style="width:870px;height:450px;" border="0" scrolling="yes" ></iframe>

我用 jquery 的attr 方法设定 iframe的src属性

$("#addOrUpdateIframe").attr("src", "http://blog.phpstudy.net/search.aspx?key=400");//可点击链接查看效果 注意页数(显示第一页数据)

没有问题,得到了我想要的结果,但是下面的地址却把key后面的参数都掉了(红色字体的参数没有取到)

$("#addOrUpdateIframe").attr("src", "http://blog.phpstudy.net/search.aspx?key=400&page=2");//可点击链接查看效果 注意页数(显示第二页数据)


郁闷中,感觉很奇怪,就用firebug看了一下修改后的值,问题找到了,jquery 把上面的地址给变成:

http://blog.phpstudy.net/search.aspx?key=400&page=2//可点击链接查看效果 注意页数(显示第一页数据)

郁闷中。。。。。。。。。。。。。。。。

找了好久也没有找到如果解决该问题,他怎么会把地址的&给改成&了呢?有点搞不懂

问题解决方法:

在无意中把

$("#addOrUpdateIframe").attr("src", "http://blog.phpstudy.net/search.aspx?key=400&page=2");//可点击链接查看效果 注意页数(显示第一页数据)

改成

$("#addOrUpdateIframe").attr("src", 'http://blog.phpstudy.net/search.aspx?key=400&page=2');//可点击链接查看效果 注意页数(显示第二页数据)

注意灰色背景的地方,把双引号改成单引号了,问题解决。
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3