Javascript函数中的HTML无法正常工作

时间:2021-12-29 21:27:46

I am using the following JavaScript function:

我使用以下JavaScript函数:

function Projects()
{
    var PrjTb1 = "<input type=text id=PrjNme size=100/>"
    var PrjTb2 = "<textarea rows=5 col=200 wrap=hard></textarea>"
    var Info = "1. Project or activity name:<br>" + PrjTb1 + "<br><br>2. Project or activity description:<br>" + PrjTb2 
    if (document.getElementById('PrjNo').value == 1)
    {
        document.getElementById('AllPrj').innerHTML = "<b>Project or activity 1.</b><br><br>" + Info
    }
}

This function executes well, after an onclick event; however, certain portions of the HTML within the declared variables are not working. For example, within the variable PrjTb2, my textarea's rows and columns do not change. Also, I cannot add the HTML tags <dd> and </dd> anywhere within the variables to create an indentation. What is interesting is that both the textarea properties and the HTML tags, <dd> and </dd>, work within the body of my form, just not in my JS function. Would anyone know what I am doing wrong.

在onclick事件之后,此函数执行良好;但是,声明的变量中的HTML的某些部分不起作用。例如,在变量PrjTb2中,我的textarea的行和列不会改变。此外,我无法在变量中的任何位置添加HTML标记

和 来创建缩进。有趣的是textarea属性和HTML标记
和 都在我的表单体内工作,而不是在我的JS函数中。谁会知道我做错了什么。

Thank you,

DFM

3 个解决方案

#1


It could be that your browser is caching your javascript. Try clearing your cache.

可能是您的浏览器正在缓存您的JavaScript。尝试清除缓存。

#2


Not sure if its the problem, but I'd enclose all your parameters in quotes.

不确定是否存在问题,但我会将所有参数括在引号中。

<input type=text id=PrjNme size=100/>

should really be:

应该是:

<input type='text' id='PrjNme' size='100' />

additionally, depending on your doctype, you may want to change

另外,根据您的doctype,您可能想要更改

<br>

to

<br />

Things that are allowed in direct HTML are not necessarily allowed using innerHTML. For example, in Firefox, try embedding a form within a form in js vs html, or in IE, try putting a block element inside an inline element. They both work fine if you have it in HTML, but if you try it with innerHTML, both browsers will complain.

使用innerHTML不一定允许直接HTML中允许的内容。例如,在Firefox中,尝试在js vs html中的表单中嵌入表单,或者在IE中,尝试将块元素放在内联元素中。如果你在HTML中使用它们,它们都可以正常工作,但是如果你用innerHTML进行尝试,那么两个浏览器都会抱怨。

#3


I don't know if your syntax error are due to copying you code here, but here is a couple if things I noticed in your code.

我不知道你的语法错误是否是由于你在这里复制你的代码,但如果我在你的代码中注意到了这一点,那么这里有几个。

  • Are you omitting semi-columns at the end of you lines on purpose?
  • 您是否故意省略了行尾的半列?

  • HTML proprieties should be surrounded by quotation marks. (Although not necessary)
  • HTML礼节应该用引号括起来。 (虽然没必要)

Also regarding the <dd></dd>, did you put those inside a definition list (dl)? Because they shouldn't be used as standalone tags.

关于

,您是否将它们放在定义列表(dl)中?因为它们不应该用作独立标签。

#1


It could be that your browser is caching your javascript. Try clearing your cache.

可能是您的浏览器正在缓存您的JavaScript。尝试清除缓存。

#2


Not sure if its the problem, but I'd enclose all your parameters in quotes.

不确定是否存在问题,但我会将所有参数括在引号中。

<input type=text id=PrjNme size=100/>

should really be:

应该是:

<input type='text' id='PrjNme' size='100' />

additionally, depending on your doctype, you may want to change

另外,根据您的doctype,您可能想要更改

<br>

to

<br />

Things that are allowed in direct HTML are not necessarily allowed using innerHTML. For example, in Firefox, try embedding a form within a form in js vs html, or in IE, try putting a block element inside an inline element. They both work fine if you have it in HTML, but if you try it with innerHTML, both browsers will complain.

使用innerHTML不一定允许直接HTML中允许的内容。例如,在Firefox中,尝试在js vs html中的表单中嵌入表单,或者在IE中,尝试将块元素放在内联元素中。如果你在HTML中使用它们,它们都可以正常工作,但是如果你用innerHTML进行尝试,那么两个浏览器都会抱怨。

#3


I don't know if your syntax error are due to copying you code here, but here is a couple if things I noticed in your code.

我不知道你的语法错误是否是由于你在这里复制你的代码,但如果我在你的代码中注意到了这一点,那么这里有几个。

  • Are you omitting semi-columns at the end of you lines on purpose?
  • 您是否故意省略了行尾的半列?

  • HTML proprieties should be surrounded by quotation marks. (Although not necessary)
  • HTML礼节应该用引号括起来。 (虽然没必要)

Also regarding the <dd></dd>, did you put those inside a definition list (dl)? Because they shouldn't be used as standalone tags.

关于

,您是否将它们放在定义列表(dl)中?因为它们不应该用作独立标签。