Html 是否有方法获取调用脚本标记的位置?
我正在尝试创建一个HTML页面,并用一种简单的方法创建复杂的元素,返回一个DOM元素。但我不知道如何将元素附加到适当的位置。例如,我想在我的Html 是否有方法获取调用脚本标记的位置?,html,Html,我正在尝试创建一个HTML页面,并用一种简单的方法创建复杂的元素,返回一个DOM元素。但我不知道如何将元素附加到适当的位置。例如,我想在我的中的段落之间使用函数,但我不知道如何使用 以下是一个例子: p1 p2 document.getlocation().append(DOM元素) p3 应该让步 p1 p2 DOM element p3 非常感谢您提供的任何帮助,包括文档和/或代码等。您需要在html中创建一个标记,在其中插入元素或文本。抓取它,然后更改innerHTML或附加子
中的段落之间使用函数,但我不知道如何使用
以下是一个例子:
p1
p2
document.getlocation().append(DOM元素)
p3
应该让步
p1
p2
DOM element
p3
非常感谢您提供的任何帮助,包括文档和/或代码等。您需要在html中创建一个标记,在其中插入元素或文本。抓取它,然后更改innerHTML或附加子项。如果是后者,您可能希望使用p或div标记而不是span
var sp=document.getElementById('here');
sp.style.display='block';
sp.innerHTML='DOM元素'代码>
p1
p2
p3
您需要在html中创建一个标记,用于插入元素或文本。抓取它,然后更改innerHTML或附加子项。如果是后者,您可能希望使用p或div标记而不是span
var sp=document.getElementById('here');
sp.style.display='block';
sp.innerHTML='DOM元素'代码>
p1
p2
p3
您可以使用
p1
p2
document.write(“DOM元素””);
p3
您可以使用
p1
p2
document.write(“DOM元素””);
p3
查看您想要的结果后,我注意到您的代码中存在多个错误。首先,DOM元素
必须用单引号或双引号括起来,才能将其视为字符串
其次,document.getlocation()
不是一个函数。要使用document.write()
将'DOM element'
放置在调用脚本的位置,实际上需要做什么
p1
p2
document.write('DOM元素');
p3
这将成功地提供所需的输出。与PHP中的echo
类似,document.write()
可用于将数据写入调用脚本的文档。查看所需结果后,我注意到代码中存在多个错误。首先,DOM元素
必须用单引号或双引号括起来,才能将其视为字符串
其次,document.getlocation()
不是一个函数。要使用document.write()
将'DOM element'
放置在调用脚本的位置,实际上需要做什么
p1
p2
document.write('DOM元素');
p3
这将成功地提供所需的输出。与PHP中的echo
类似,document.write()
可用于将数据写入调用脚本的文档。我会给每个HTML元素一个id,然后按照下面的代码进行操作。我已经给了你2个示例方法和文档链接来尝试。
希望有帮助:)
p1
p2
p3
//示例1:创建元素并在另一个元素之前插入:
//见:https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
让newElement1=document.createElement('p');
newElement1.innerText='新的第1段';
设el3=document.getElementById('id3');
文档.正文.插入前(新元素1,el3);
//示例2:创建一个元素并使用InsertAdjaceEntelement函数:
//见:https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement
让newElement2=document.createElement('p');
newElement2.innerText='新的第2段';
设el5=document.getElementById('id5');
el5.插入相邻元素(“后置”,新元素2);
我会给每个HTML元素一个id,然后按照下面的代码进行操作。我已经给了你2个示例方法和文档链接来尝试。
希望有帮助:)
p1
p2
p3
//示例1:创建元素并在另一个元素之前插入:
//见:https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
让newElement1=document.createElement('p');
newElement1.innerText='新的第1段';
设el3=document.getElementById('id3');
文档.正文.插入前(新元素1,el3);
//示例2:创建一个元素并使用InsertAdjaceEntelement函数:
//见:https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement
让newElement2=document.createElement('p');
newElement2.innerText='新的第2段';
设el5=document.getElementById('id5');
el5.插入相邻元素(“后置”,新元素2);
哦,是的,我并不是说它是一个排序字符串,而是一种实际dom元素的伪代码。那么,是否要向该元素添加另一个元素?您也可以在字符串中使用标记。例如,document.write('Bold')
会将文本Bold
写入b
标记中以使其加粗。哦,是的,我不是说它是一个排序字符串,而是一个实际dom元素的伪代码。那么,是否要在此元素中添加另一个元素?您也可以在字符串中使用标记。例如,document.write('Bold')
将文本Bold
写入b
标记中,使其变为粗体。
<body>
p1
<br>
p2
<br>
<script>document.write('<p>DOM Element</p>');</script>
<br>
p3
</body>
<body>
p1
<br>
p2
<br>
<script>document.write('DOM element');</script>
<br>
p3
</body>
<!DOCTYPE html>
<html>
<body>
<p id="id1">p1</p>
<br id="id2">
<p id="id3">p2</p>
<br id="id4">
<br id="id5">
<p id="id6">p3</p>
<script>
// Example 1: create element and insert before another element:
// See: https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
let newElement1 = document.createElement('p');
newElement1.innerText='New paragraph 1';
let el3 = document.getElementById('id3');
document.body.insertBefore(newElement1, el3);
// Example 2: create an element and use the insertAdjacentElement function:
// See: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement
let newElement2 = document.createElement('p');
newElement2.innerText='New paragraph 2';
let el5 = document.getElementById('id5');
el5.insertAdjacentElement('afterend', newElement2);
</script>
</body>
</html>