javascript - 访问iframe的元素

时间:2016-06-08 07:34:54

标签: javascript html

我需要更改框架元素的样式,但iframe没有id或类或名称

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using MySql.Data.MySqlClient;
using System.Windows.Forms.DataVisualization.Charting;
using System.Runtime.InteropServices;


namespace weatherstation1
{
    public partial class Form2 : Form
    {

        string Town;
        string Temperature;
        string Temperature2;
        string Condition;
        string Humidity;
        string Windspeed;
        string ConditionCode;
        int cityNumber = 44418;

        public Form2()
        {
            InitializeComponent();
            Getweather();
            GetCondition();
            textBox1.Text = Town;
            textBox3.Text = Condition;
            textBox4.Text = Humidity;
            textBox5.Text = Windspeed;


        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frmAbout = new AboutBox1();
            frmAbout.Show();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutBox1 frmAbout = new AboutBox1();
            frmAbout.Show();
        }


        private void Getweather()
        {

            string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");

            XmlDocument wData = new XmlDocument();
            wData.Load(query);

            XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
            manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
            XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
            XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);

            Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
            Temperature2 = ((Double.Parse(Temperature) - 32) / 1.8).ToString();
            Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
            ConditionCode = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["code"].Value;
            Time = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["date"].Value;
           // Windspeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
           // Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;




        }

        private void database()
        {
            // string Time = DateTime.Now.ToString("d/M/yyyy HH:mm");
            string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456";
            MySqlConnection MyConn = new MySqlConnection(MyConnection);
            string query1 = "INSERT INTO weatherstation1.London"+
                                            "(City, Time, Temperature, Temperature2, WeatherCondition) VALUES('" + Town +
                                             "','" + Time + "','" + Temperature + "','" + Temperature2 + "' + Condition + "');";
            MySqlCommand MyCommand = new MySqlCommand(query1, MyConn);
            MySqlDataReader MyReader;
            MyConn.Open();
            MyReader = MyCommand.ExecuteReader();





        }

        public void GetCondition()
        {
            string cc = ConditionCode;
            if (cc <= 'Showers')
            {
                weatherCondition.ImageLocation = "showers.png";
            }
            else if (cc <= 'Scattered Showers')
            {
                weatherCondition.ImageLocation = "scatteredshowers.png";
            }
            else if (cc  <= 'Mostly Cloudy')
            {
                weatherCondition.ImageLocation = "mostlycloudy.png";
            }
            else if (cc <= 'Rain')
            {
                weatherCondition.ImageLocation = "rain.png";
            }

            else if (cc <= 'Mostly Cloudy')
            {
                weatherCondition.ImageLocation = "mostlycloudy.png";
            }
            else if (cc <= 'Rain')
            {
                weatherCondition.ImageLocation = "rain.png";
            }
            else if (cc >= 'Scattered Thunderstorms')
            {
                weatherCondition.ImageLocation = "scatteredthunderstorms.png";
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {


        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Getweather();
            textBox1.Text = Town;
            textBox3.Text = Condition;
            //textBox4.Text = Humidity;
            //textBox5.Text = Windspeed;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            database();     
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            database();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void graph()
        {
            if (radioButton2.Checked)
            {
                MySqlDataReader MyReader2;
                string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection);
                MyConn2.Open();
                string query2 = " SELECT * FROM  London   ";
                MySqlCommand MyCommand2 = new MySqlCommand(query2, MyConn2);
                MyReader2 = MyCommand2.ExecuteReader();

                while (MyReader2.Read())
                {
                    chart1.Series["Series1"].Points.AddXY(MyReader2.GetString("Time"), MyReader2.GetString("Temperature"));
                    chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
                    chart1.Series["Series1"].Color = Color.Blue;
                }
            }
            if (radioButton1.Checked)
            {

                MySqlDataReader MyReader2;
                string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection);
                MyConn2.Open();
                string query2 = " SELECT * FROM  London ";
                MySqlCommand MyCommand2 = new MySqlCommand(query2, MyConn2);
                MyReader2 = MyCommand2.ExecuteReader();
                while (MyReader2.Read())
                {
                    chart1.Series["Series1"].Points.AddXY(MyReader2.GetString("Time"), MyReader2.GetString("Temperature2"));
                    chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
                    chart1.Series["Series1"].Color = Color.Blue;
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            graph();
            Getweather();
            database();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            graph();
            Getweather();
            database();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            double celc = (Int32.Parse(Temperature) - 32) / (1.8);
            string Temperature2 = celc.ToString();
            textBox2.Text = Temperature2;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.Text = Temperature;
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            cityNumber = Int32.Parse(textBox7.Text);
            timer1.Interval = Int32.Parse(textBox8.Text);
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            Getweather();
            toolStripTextBox1.Text = "Temperature:" + Temperature + " F";
        }

        private void weatherCondition_Click(object sender, EventArgs e)
        {

        }


       }
        }

For database in localhost
 CREATE TABLE London
 (ID INT NOT NULL AUTO_INCREMENT, City VARCHAR(30) NOT NULL, Time       VARCHAR(20) NOT NULL, Temperature VARCHAR(5) NOT NULL, Temperature2 VARCHAR(5) NOT NULL,  WeatherCondition VARCHAR(30) NOT NULL, PRIMARY KEY (ID));

javascript函数中的错误是什么?

2 个答案:

答案 0 :(得分:1)

您好我认为您必须获取节点列表的第一个元素,请参阅http://www.w3schools.com/jsref/met_document_getelementsbytagname.asp

换句话说,你必须这样做:

parent.document.getElementsByTagName("iframe")[0].contentDocument.querySelector(".login").style.display = 'none';

答案 1 :(得分:-1)

为什么不使用jQuery? - 包括jquery脚本,然后在

之后
<script>
$('iframe').hide();
</script>