如何在每次点击时更改标题的颜色?

时间:2015-11-12 13:43:34

标签: javascript

以下是html代码,

<body onload="alert('Hello')">
    <h1>click here</h1>
</body>

下面是javascript代码,

function init(){
    var h1Tags = document.getElementsByTagName("h1");
    h1Tags[0].onclick = changeColor;
}

function changeColor(){
    this.innerHTML = "Click Again"

    /*en.wikipsedia.org/wiki/List_of_colors:_A_F */
    var randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);

    this.style.color = randomColor;
}

document[onload] = init;

以上代码可以改变标题的颜色。

此代码有什么问题?

2 个答案:

答案 0 :(得分:3)

问题很可能在这一行:

public void Convert(string fileNames) {
    int pageCount = 0;
    iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(fileNames);
    pageCount = reader.NumberOfPages;
    string ext = System.IO.Path.GetExtension(fileNames);

    //string[] outfiles = new string[pageCount];
    //Excel.Application app = new Excel.Application();
    //app.Workbooks.Add("");
    CSVExtractor extractor = new CSVExtractor();
    //string outfilePDF1 = fileNames.Replace((System.IO.Path.GetFileName(fileNames)), (System.IO.Path.GetFileName(fileNames).Replace(".pdf", "") + "_rez" + ".csv"));
    string outfilePDFExcel1 = fileNames.Replace((System.IO.Path.GetFileName(fileNames)),
        (System.IO.Path.GetFileName(fileNames).Replace(".pdf", "") + "_rez" + ".xls"));
    extractor.RegistrationName = "demo";
    extractor.RegistrationKey = "demo";

    string folderName = @"C:\Users\Dafina\Desktop\PDF_EditProject\PDF_EditProject\PDFs";
    string pathString = System.IO.Path.Combine(folderName, System.IO.Path.GetFileName(fileNames).Replace(".pdf", "")) + "-CSVs";
    System.IO.Directory.CreateDirectory(pathString);
    for (int i = 0; i < pageCount; i++)
    {
        string outfilePDF = fileNames.Replace((System.IO.Path.GetFileName(fileNames)),
            (System.IO.Path.GetFileName(fileNames).Replace(".pdf", "") + "_" + (i + 1).ToString()) + ext);
        extractor.LoadDocumentFromFile(outfilePDF);
        //string outfile = fileNames.Replace((System.IO.Path.GetFileName(fileNames)),
        //    (System.IO.Path.GetFileName(fileNames).Replace(".pdf", "") + "_" + (i + 1).ToString()) + ".csv");
        string outfile = fileNames.Replace((System.IO.Path.GetFileName(fileNames)),
            (System.IO.Path.GetFileName(fileNames).Replace(".pdf", "") + "-CSVs\\" + "Sheet_" + (i + 1).ToString()) + ".csv");
        extractor.SaveCSVToFile(outfile);
    }
    Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

    if (xlApp == null)
    {
        Console.WriteLine("Excel is not properly installed!!");
        return;
    }

    Excel.Workbook xlWorkBook;


    object misValue = System.Reflection.Missing.Value;
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    string[] cvsFiles = Directory.GetFiles(pathString);
    Array.Sort(cvsFiles, new AlphanumComparatorFast());

    //string[] lista = new string[pageCount];
    //for (int t = 0; t < pageCount; t++)
    //{
    //    lista[t] = cvsFiles[t];           
    //}

    //Array.Sort(lista, new AlphanumComparatorFast());


    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    for (int i = 0; i < cvsFiles.Length; i++)
    {
        int sheet = i + 1;
        xlWorkSheet = xlWorkBook.Sheets[sheet];

        if (i < cvsFiles.Length - 1)
        {
            xlWorkBook.Worksheets.Add(Type.Missing, xlWorkSheet, Type.Missing, Type.Missing);
        }


        int sheetRow = 1;
        Encoding objEncoding = Encoding.Default;
        StreamReader readerd = new StreamReader(File.OpenRead(cvsFiles[i]));
        int ColumLength = 0;
        while (!readerd.EndOfStream)
        {
            string line = readerd.ReadLine();
            Console.WriteLine(line);
            try
            {
                string[] columns = line.Split((new char[] { '\"' }));

                for (int col = 0; col < columns.Length; col++)
                {
                    if (ColumLength < columns.Length)
                    {
                        ColumLength = columns.Length;
                    }
                    if (col % 2 == 0)
                    {

                    }
                    else if (columns[col] == "")
                    {

                    }
                    else
                    {
                        xlWorkSheet.Cells[sheetRow, col + 1] = columns[col].Replace("\"", "");
                    }
                }
                sheetRow++;
            }
            catch (Exception e)
            {
                string msg = e.Message;
            }
        }

        int k = 1;
        for (int s = 1; s <= ColumLength; s++)
        {
            xlWorkSheet.Columns[k].Delete();
            k++;
        }



        releaseObject(xlWorkSheet);
        readerd.Close();
    }

    xlWorkBook.SaveAs(outfilePDFExcel1, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
        misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
        misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);

    xlApp.Quit();

    releaseObject(xlWorkBook);
    releaseObject(xlApp);

    var dir = new DirectoryInfo(pathString);
    dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
    dir.Delete(true);

}
在这种情况下,

document[onload] = init 是指(未定义)变量,该值用作属性键。所以它实际上会被评估为:

onload

您必须将其替换为:

document[undefined] = init

document['onload'] = init

答案 1 :(得分:-2)

我建议您使用window.onload代替window.onload vs document.onload

function init(){
    var h1Tags = document.getElementsByTagName("h1");
    h1Tags[0].onclick = changeColor;
}

function changeColor(){
    this.innerHTML = "Click Again"

    /*en.wikipsedia.org/wiki/List_of_colors:_A_F */
    var randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);

    this.style.color = randomColor;
}

// The line below was your problem
window.onload = init;
/* This may be helpful for you */
h1 {
    -webkit-user-select: none; /* webkit (safari, chrome) browsers */
    -moz-user-select: none; /* mozilla browsers */
    -khtml-user-select: none; /* webkit (konqueror) browsers */
    -ms-user-select: none; /* IE10+ */
}
<h1>Click here</h1>

另请注意我添加的CSS,如果人们快速点击,则不会同时选择文本。