简体   繁体   English

javascript函数,如何在document.write中获取变量?

[英]javascript function, how to take the variable within document.write?

I need to call a variable inside a document.write but it doesnt work... example 我需要在document.write调用一个变量,但它不起作用......例子

function(){
    var variable=document.getElementById("text");
    alert("your text "+ variable);
}

inside a table there is: 在一张桌子里面有:

document.write('<td><input type="text" id="example"></td>');<br>
document.write('<td><input type="button" value="enter a text" onclick="function()">

Anonymous functions like below are allowed in javascript but we cannot call them by using function(){} from an element etc at a later time. javascript中允许使用下面的匿名函数,但我们不能通过稍后使用元素等function(){}来调用它们。 They need to have a reference, in your case just a name after function will do 他们需要有一个参考,在你的情况下,只需要一个function后的名称

function(){        
  //Your code
} 

Becomes

function myFunction(){ //myFunction can be changed to another more suitable name
  //Your code here;
}

Then from your document.write statement call your named function in the onclick event 然后从document.write语句中调用onclick事件中的命名函数

document.write('<td><input type="text" id="example"></td>');<br>
document.write('<td><input type="button" value="enter a text" onclick="myFunction()"> 

now that you aren't using function() which is a reserved word in javascript but using myFunction() which javascript now thinks is your named function, it should work 既然你没有使用function()这是javascript中的保留字,但使用myFunction() javascript现在认为是你的命名函数,它应该工作

function是保留关键字,不能将其用作函数名。

var newFunction = function(){
    var variable=document.getElementById("text");
    alert("your text "+ variable);
}
document.write('<td><input type="button" value="enter a text" onclick="newFunction()">

You'll need to reference which property of the Element you want to read. 您需要引用要阅读的Element的哪个属性。 Simply using getElementById will return the object, not the value of the textfield. 简单地使用getElementById将返回对象,而不是textfield的值。 Use getelementById('text').value . 使用getelementById('text').value

Not sure how much of your code there is real, and how much is just an example, but you need to double-check for your getElementById() and check that the ID you are providing is actually in the HTML. 不确定你的代码中有多少是真实的,多少只是一个例子,但你需要仔细检查你的getElementById()并检查你提供的ID是否实际上在HTML中。

In your example you show getElementById("text") but then don't have any HTML with an ID of text 在您的示例中,您显示了getElementById("text")但是没有任何带有text ID的HTML

Also, if you want to extract the content that is being stored inside that variable, you might want to get its value via getElementById("text").value 此外,如果要提取存储在该变量中的内容,您可能希望通过getElementById("text").value获取其值getElementById("text").value

The main issue though, seems to be that you're using function(){} - you should name your function, for instance: function foo(){} and then use onclick='foo()' to call it. 但主要的问题似乎是你正在使用function(){} - 你应该命名你的函数,例如: function foo(){}然后使用onclick='foo()'来调用它。

function is a reserved keyword. function是保留关键字。

You can use it like this 你可以像这样使用它

var func = function(){
    var variable=document.getElementById("text");
    alert("your text "+ variable);
}

onclick="func()"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM