English 中文(简体)
哪怕是基本 j子的工作问题[此处注]
原标题:Problems getting even basic javascript to work [noob here]
  • 时间:2012-04-28 01:32:59
  •  标签:
  • javascript

因此,我试图学习 j。 我走过了几门教导和指南,但是他们常常不教你如何书写 j,以便用网页开展工作。

我要学习的是,如何做到这一点,与我最近刚刚开始的网站提供了一些基本互动。 虽然它们含有动态内容,但我仍然希望打字片的某些互动性。 然而,我的所有尝试都是徒劳的,因为没有任何东西可以奏效。 我的最终目标是使我的网站更加专业,并可能得到一个节点。js服务器站起来,与一些雅氏或网状一道运行,也许会与信道建立两条游戏,但远晚了。

下面的法典是我根据读到的Tow DOM 辅导材料一撰写的一些基本内容。 问题完全没有工作。 因此,任何人都能够告诉我,它为什么根本不工作。 是的,我确实在我的浏览器上翻了 j。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>
    <p>SomeText</p>
    <script type="text/javascript">
        var ptag = document.getElementById( p );

        function alertclick() {
            alert(this);
        }

        for (i = 0; i < ptag.length; i++){
            var attach = ptag[i];
            attach.addEventListener("click", alertclick, false);
        }


    </script>
</body>
</html>
问题回答

这是因为你重新使用<代码>getElementById,而该编码与<代码>p无关。 相反,使用<条码>所有<条码>p要素。

var ptags = document.getElementsByTagName( p );

for (var i = 0; i < ptags.length; i++){
     var element = ptags[i];
     element.addEventListener("click", alertclick, false);
}

Have some confidence! You are almost there. This is the behavior I understood you want: click on a paragraph to alert its contents.

我们喜欢在两个问题和答案中显示一流:。 http://jsfiddle.net/bxvny/

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>
    <p>SomeText</p>
    <p>SomeText2</p>
    <p>SomeText3</p>
    <script type="text/javascript">
        var ptag = document.getElementsByTagName( p );

        function alertclick() {
            alert(this.innerHTML);
        }

        for (i = 0; i < ptag.length; i++){
            var attach = ptag[i];
            attach.addEventListener("click", alertclick, false);
        }


    </script>
</body>
</html>

There s a few things that will catch you out as a beginner.

  1. When the JavaScript runs before the page has loaded.

  2. The order in which you include JavaScript files in the page is important.

  3. 你可以做些什么。

Plain JavaScript:

在您的法典中,假定你重新尝试使用所有<代码><p> tags, 您可使用getElemenysByTagName( p )

如果你要查阅一个单一的要素,我建议把一个身份证贴在<p id=“me”>SomeText</p>,然后使用getElementsById(我)

如果你不敢接触一套内容,但并不是所有一类内容,你应适用一个类别:<代码><p等=“组”>SomeText</p>. 您可使用getElementsByClassName(组),但只得到较现代浏览器的支持。

For Selection of elements I strongly recommend using jQuery, it makes the process much easier and more flexible. http://jquery.com/

jQuery:

Once using jQuery you can access elements as so:

$( #me ); //Individual Element by ID
$( .group ); //Group of elements by Class
$( <p> ); //All P elements

j 创造活动处理器时,质量也非常好,因为它简化和交叉浏览器兼容。 阁下:

rel=“nofollow”>http://api.jquery.com/click/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签