小编Cur*_*arn的帖子

有谁可以请解释这个JavaScript代码中发生了什么?

我希望浏览器显示自上次刷新以来的秒数.我不明白为什么Code 1不起作用; 守则2; 如果代码1不起作用,为什么代码3有效?该setInterval呼叫是在码1和码3的方式类似定义的函数是不同的.但我不清楚为什么会有所作为.

非常感谢您的帮助.我刚开始学习JavaScript.

代码1

<html>
<head>
    <title>Time Since Refresh</title>
</head>
<body>
<br/>
<br/>
<span id="timeSinceStart"></span>

<script language="JavaScript">
    var timeRefreshed = new Date();
    function displayTimeSinceStart(){
        var now = new Date();
        //compute elapsed time
        var elapsed = Math.round((now - timeRefreshed)/1000);
        document.getElementById("timeSinceStart").innerHTML = "Time Elapsed: " + elapsed + " seconds."; 
    }

    // Update seconds counter
    setInterval(displayTimeSinceStart(), 1000);

</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

代码2 与CODE 1相同,但setInterval()函数写为

setInterval("displayTimeSinceStart();", 1000);
Run Code Online (Sandbox Code Playgroud)

代码3

<html>
<head>
    <title>Time Since Refresh</title>
</head>
<body>
<br/>
<br/>
<span …
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
95
查看次数

改变链接的属性

我有以下html文字.

<ol class="otherstufflinks">
    <li> 
        <a href="./pythonNotes/index.html">Python notes</a>
    </li>
    <li> 
        <a href="./figure.html">Cool figure made using</a> 
        <a href="http://mbostock.github.com/d3/" class="nestedlink">d3.js library.</a>
    </li>             
</ol>
Run Code Online (Sandbox Code Playgroud)

css文件中的相关部分是:

a:link, a:visited {
    color: steelblue;
    text-decoration: none;
}

a:hover {
    color: rgb(210,135,60);
}

.otherstufflinks > li,
.otherstufflinks a:link, .otherstufflinks a:visited 
{
    font-weight: normal;
    color: rgb(75,75,75);
}

.otherstufflinks a:hover  {
    color: rgb(210,135,60)
}
Run Code Online (Sandbox Code Playgroud)

我想选择一种不同的颜色,比如红色,用于与班级的链接nestedlink.如果有人能告诉我该怎么做,我将不胜感激?我尝试了以下但没有一个工作.

第一次尝试:

.nestedlink a:link, .nestedlink a:visited {
    color: red;
}
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

.otherstufflinks .nestedlink a:link, .otherstufflinks .nestedlink a:visited {
        color: red;
   } …
Run Code Online (Sandbox Code Playgroud)

css css-selectors

0
推荐指数
1
解决办法
44
查看次数

为什么函数不返回值?

你能看看这个功能,chooseSampleKey并帮助我理解它为什么不总是返回一个值?它有时返回一个值,而在其他时候返回None,所以你必须运行它几次.问题尤其在我命名为问题分支的分支中.在我看来,代码应该总是返回一个值,但要么我在某处出错,要么在Python中有一个错误.

import random

peeps = {1: [], 2: [], 3: ['A', 'B'], 4: ['C', 'D'], 5: ['E']}

dictkeys = [1, 2, 4, 5]

def chooseSampleKey(peeps, dictkeys):
    if len(dictkeys) > 0:
        sampleKey = random.sample(dictkeys, 1)[0]
        print 'From chooseSampleKey, sampleKey = ', sampleKey
        print "peeps in samplekey: ", len( peeps[sampleKey] )

        if len( peeps[sampleKey] ) > 0:

            # **problem branch**: sampleKey is defined and has a value.
            # the function comes here and prints the following statement
            # with the …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
101
查看次数

为函数应用于分数时显示的负数指定的错误.为什么?

我正在尝试学习Haskell,并且我定义了以下简单的递归函数来计算阶乘.

fact n | n < 0 = error "fact only valid for non-negative integers"
       | n == 0 = 1
       | n > 0 = n * fact(n-1)
Run Code Online (Sandbox Code Playgroud)

它适用于正整数,并且正如预期的那样,当使用负整数调用时,它会抛出我指定的错误.

问题是什么?:当我尝试将它应用于Fractional时,它给了我同样的错误("事实只对非负整数有效"),例如fact 10.5.为什么它给了我同样的错误,我明确指出应该只适用于n <0的情况.

recursion haskell

0
推荐指数
1
解决办法
51
查看次数

标签 统计

css ×1

css-selectors ×1

haskell ×1

javascript ×1

python ×1

recursion ×1