admin管理员组文章数量:1424445
I have a page that is pulling in via an iframe a page of html content from a pre-existing content management system, whose content I can not directly change. I want to use jquery to alter some of the links and button locations dynamically.
For links within the page I was able to change the href attr values of
<a href="/content.asp">more</a>
from content.asp to content2.asp using
$("a[href^='/content.asp']")
.each(function() { this.href = this.href.replace("content.asp", "content2.asp"); });
But the page pulled in also has form buttons that contains javascript onclick functions e.g.
<INPUT type="button" value="Add to basket" onClick="document.location='/shopping-basket.asp?mode=add&id_Product=1076';">
I basically want to use jquery to select any such buttons and change the shopping-basket.asp to shopping-basket2.asp
How can I select these buttons/onclick functions and change the location string?
I have a page that is pulling in via an iframe a page of html content from a pre-existing content management system, whose content I can not directly change. I want to use jquery to alter some of the links and button locations dynamically.
For links within the page I was able to change the href attr values of
<a href="/content.asp">more</a>
from content.asp to content2.asp using
$("a[href^='/content.asp']")
.each(function() { this.href = this.href.replace("content.asp", "content2.asp"); });
But the page pulled in also has form buttons that contains javascript onclick functions e.g.
<INPUT type="button" value="Add to basket" onClick="document.location='/shopping-basket.asp?mode=add&id_Product=1076';">
I basically want to use jquery to select any such buttons and change the shopping-basket.asp to shopping-basket2.asp
How can I select these buttons/onclick functions and change the location string?
Share Improve this question edited Jan 22, 2009 at 18:38 Crescent Fresh 117k27 gold badges157 silver badges140 bronze badges asked Jan 22, 2009 at 18:36 Mark PeachMark Peach5 Answers
Reset to default 3Not very proud of this solution, but it works....
var getid = /id_Product=(\d+)/;
$('input[type="button"]').each(function() {
var onclick = String(this.onclick);
var id = onclick.match(getid);
if(id) {
this.onclick = function() {
window.location = "/shopping-basket2.asp?mode=add&id_Product=" + id[1];
};
}
});
http://docs.jquery./Selectors/attributeContains#attributevalue
$("input[onClick*='shopping-basket.asp']").each( ... )
Just thought that I would update this answer as I have tried several bination in JQuery 1.4.1
1) onclick= function() - not working
$("input").each(function() {
$(this).onclick = function() {
window.location.href= 'http://www.google.';
return false;
}
}
2) $newclick = eval(...) not working
$("input").each(function() {
$jscript = "window.location.href= 'http://www.google.'; return false;"
$newclick = eval("(function(){" + $jscript + "});");
$(this).onclick = newclick;
}
btw. $newclick is evaluated to be undefined
3) the new answer that works in JQuery 1.4.1
$("input").each(function() {
$(this).click(function() {
window.location.href = "http://www.google.";
return false;
});
I searched the JQuery API and there is no onclick documentation. Looks like click() is the way to go. Let me know if I have made any mistake(s).
Cheers
Give your button a unique id
and then reference it from a javascript script with:
document.getElementById("idOfButton").onClick
Simply set the value to a new text string.
Attributes are changed with the .attr method.
$("a").each(function(){
$(this).attr("href","content.asp");
});
You should be able to replace hard-coded-onClick text as well. To select those buttons, you can either grab all of them:
$("input.convertMe").each(function(){...});
Or you can add classes to the buttons you are specifically interested in:
<input type="button" class="convertMe"...>
Once you've done either of those two, you simply run through each testing their onClick attribute, and determining if you need to do any changes:
$("input.convertMe").each(function(){
// Test onClick attr, and replace string.
});
本文标签: javascriptChange the value of an existing onclick functionStack Overflow
版权声明:本文标题:javascript - Change the value of an existing onclick function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745369993a2655689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论