如何使用javascript获取嵌套iframe的内部内容?

时间:2016-01-01 07:27:48

标签: javascript jquery

这是我的代码。现在,我想获取嵌套帧的内容。我怎样才能做到这一点?

<iframe id="frame1"> //outer frame
    <body>
        <div>Some inner contnet</div>
        <iframe id="nestedIframe"> //inner frame
            <body> //inner html goes here
                <div>Inner content of the nested frame</div>
            </body>
        </iframe>
    </body>
</iframe>

1 个答案:

答案 0 :(得分:1)

尝试以下操作。

import UIKit
import Parse

class SignUpViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!
@IBOutlet var EmailTextField: UITextField!

var ActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
var currentTextField : UITextField?

func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.UsernameTextField.delegate = self
    self.PasswordTextField.delegate = self
    self.EmailTextField.delegate = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func BackToFirstPageButton(sender: AnyObject) {

    performSegueWithIdentifier("BackToLogInPage", sender: self)

}

func SignOutForEmailVerification() {

    PFUser.logOut()
    performSegueWithIdentifier("BackToLogInPage", sender: self)
}

@IBAction func SignUpButton(sender: AnyObject) {

    if UsernameTextField.text == "" || PasswordTextField.text == "" || EmailTextField.text == "" {

        let SignUpAlert = UIAlertController (title: "Error in form", message: "Please fill in the blanks", preferredStyle: UIAlertControllerStyle.Alert)

        SignUpAlert.addAction((UIAlertAction(title: "Dismiss", style: .Default, handler: { (action) -> Void in

        })))

        self.presentViewController(SignUpAlert, animated: true, completion: nil)

    } else {

        currentTextField?.resignFirstResponder();

        ActivityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
        ActivityIndicator.center = self.view.center
        ActivityIndicator.hidesWhenStopped = true
        ActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        view.addSubview(ActivityIndicator)
        ActivityIndicator.startAnimating()
        UIApplication.sharedApplication().beginIgnoringInteractionEvents()

        var user = PFUser()
        user.username = UsernameTextField.text
        user.password = PasswordTextField.text
        user.email = EmailTextField.text

        user.signUpInBackgroundWithBlock({ (success, error) -> Void in

            self.ActivityIndicator.stopAnimating()
            UIApplication.sharedApplication().endIgnoringInteractionEvents()

            if error == nil {

                let EmailVerificationAlert = UIAlertController(title: "Email Verification", message: "Please click the link in an email we have just sent you", preferredStyle: UIAlertControllerStyle.Alert)

                EmailVerificationAlert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { EmailVerificationAlert in self.SignOutForEmailVerification()})
                )

                self.presentViewController(EmailVerificationAlert, animated: true, completion: nil)
            }

        })
    }

    func textFieldDidBeginEditing(textField: UITextField!){

        currentTextField = textField;

    }

    func textFieldShouldReturn(textField: UITextField!) -> Bool {

        currentTextField.resignFirstResponder()
        return true

    }

}

}

请注意,只有当相同的域名,端口,协议为父级和Iframe提供服务时,才能访问IFrame内容。否则它将成为CORS案例。在这种情况下,如果您拥有父域名和iframe域名,则可以尝试postMessage api,为了实施postMessage,您需要有权访问这两个域。