问题
I have an Iframe for a page that allows people to sign up for my email newsletters.
I want to auto fill the email field in the iframe based on the querystring url (page.html?email=email)
I know how to do the querystring stuff, I'm just not sure If I'm able to access the input text box within the frame. The form or the textbox neither have id's they only have names.
I've tried the following..
$("email_frame").contents.find('input[name=email]').append("test");
Any help would be great.
回答1:
If an IFRAME is loaded from a different domain than the main page, the 2 can't interact using JavaScript or CSS.
回答2:
You may be able to do this by passing the data as
iframe.src = 'otherdomain/frame.html#bob@foo.com'
and using it in the iframe as
window.onload = function ()
{
data = window.location.hash;
}
for more details read : http://msdn.microsoft.com/en-us/architecture/bb735305.aspx
回答3:
One trick that I've used to get around these little issues is sending an anonymous function to setTimeout():
var execMe = function() {$("email_frame").contents.find('input[name=email]').append("test");};
var result = setTimeout("execMe", 1000);
Disclaimer: I'm not certain this will work in this circumstance, and it is hacky. Use at your own risk. Just tossing around an idea :)
来源:https://stackoverflow.com/questions/2141535/change-text-box-value-of-an-iframe-on-a-different-domain-using-jquery