在JavaScript中传递价值 [英] Passing by value in JavaScript

查看:75
本文介绍了在JavaScript中传递价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是假设JavaScript将变量的副本传递给函数 - 所以当这段代码工作时我不知道为什么 - 动态创建的元素没有被返回。如果函数stylepara没有使用创建的段落的副本?



谢谢



I always assumed JavaScript passed copies of variables to functions - so while this code works I'm not sure why - the dynamically created element is not being returned. should the function stylepara not be working with a copy of the created paragraph?

Thanks

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamically Create</title>
<script>
function create()
{
	var x = document.createElement('p');
	var paragraphText = document.createTextNode("Dynamically Created Paragraph");
	x.appendChild(paragraphText);
	stylepara(x);
	document.body.appendChild(x);
}

function stylepara(para)
{
	para.style.backgroundColor="orange";
	para.style.fontFamily="calibri";
	para.style.fontSize="32px";
	para.style.color="red";
	return(true);
}

</script>
</head>
<body onload = "create()">
</body>
</html>

推荐答案

您的问题不明确,与问题说明不符。

创建函数deos不需要任何输入。它每次都在加载页面上执行。



如果你想将默认文本传递给函数,请将其更改为:

Your question is not clear and does not corresponds to the issue description.
Create function deos not require any inputs. It is executed every time on load page.

If you want to pass default text to function, change it to:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamically Create</title>
<script>
function create(partext)
{
    var x = document.createElement('p');
    var paragraphText = document.createTextNode(partext);
    x.appendChild(paragraphText);
    stylepara(x);
    document.body.appendChild(x);
}

function stylepara(para)
{
    para.style.backgroundColor="orange";
    para.style.fontFamily="calibri";
    para.style.fontSize="32px";
    para.style.color="red";
    return(true);
}

</script>
</head>
<body onload = "create('A brand new function, WOW!')">
</body>
</html>


这篇关于在JavaScript中传递价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆