简体   繁体   English

使用javascript将div元素输出到文本区域

[英]Output div element to text area using javascript

Today I'm trying to make a web program that will display div elements containing some <p> and <ul> elements inside a textarea that is initially blank. 今天,我正在尝试制作一个Web程序,该程序将在最初为空白的文本区域内显示包含一些<p><ul>元素的div元素。

Here is what I have so far, but nothing is actually getting outputted to the textarea. 这是我到目前为止的内容,但实际上没有输出到文本区域。

My jsfiddle: http://jsfiddle.net/zHWQD/1/ 我的jsfiddle: http : //jsfiddle.net/zHWQD/1/

Here is my function that I think is causing problems... 我认为这是引起问题的功能...

<script>
myFunction()
{

var subject = document.getElementById("subjectList");
var subjectSelect = subject.options[subject.selectedIndex].text;


var mathDiv = document.getElementById("math");
var paperDiv = document.getElementById("paperediting");
var outputContent = document.getElementById("output");

if (subjectSelect == "Math")
{
outputContent.innerHTML = mathDiv.innerHTML;
} else if (subjectSelect == "Paper Editing")
{
outputContent.innerHTML = paperDiv.innerHTML;
}
}
</script>

The way the code is supposed to work, or the way I thought it should work, is that I have a variable that contains the value from a dropdown menu. 代码应该工作的方式,或者我认为应该工作的方式,是我有一个包含下拉菜单中的值的变量。 (followed the instructions here ) (按照此处的说明进行操作)

Then, i grab the content of the divs, and save them to variables. 然后,我抓取div的内容,并将其保存到变量中。 ( Get content of a DIV using JavaScript ) 使用JavaScript获取DIV的内容

I then use an if statement to see if the subject selected is equal to either "Math" or "Paper Editing". 然后,我使用if语句查看所选主题是否等于“数学”或“纸张编辑”。

Depending on the selection, the content of the output is changed to equal that of the div elements. 根据选择,输出的内容将更改为等于div元素的内容。 ( How to change the Content of a <textarea> with Javascript ) 如何使用Javascript更改<textarea>的内容

Any help would be greatly appreciated! 任何帮助将不胜感激!

It appears you are forgetting the keyword function before declaring your myFunction() . 看来您在声明myFunction()之前忘记了关键字function

Because of how jsfiddle wraps your code in window.onload by default, myFunction() is not accessible to use for inline html. 由于jsfiddle默认情况下将代码包装在window.onload中的方式,因此无法将myFunction()用于内联html。 You can change this in the top left drop down in jsfiddle swapping onLoad to No wrap - in <head> 您可以在jsfiddle的左上角下拉菜单中将其更改为onLoad替换为No wrap - in <head>

I think you likely want to swap your textarea for a div as a textarea will not allow html inside of it. 我认为您可能希望将textarea换成div,因为textarea不允许在其中包含html。

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

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