Php 如何使用javascript的addChild方法将节点添加到我的div中?
你好,我有一个节目如下Php 如何使用javascript的addChild方法将节点添加到我的div中?,php,javascript,Php,Javascript,你好,我有一个节目如下 <div id="test" class="test2"> <p> <a href=""> <a> </p> <p> <a href=""> <a> </p> <p> <a href=""> <a> </p> <p> <a href=""> <a> </p> </d
<div id="test" class="test2">
<p> <a href=""> <a> </p>
<p> <a href=""> <a> </p>
<p> <a href=""> <a> </p>
<p> <a href=""> <a> </p>
</div>
(我已经删除了数据,但这是结构)
如何使用javascript的addChild函数添加新节点
<p> <a href=""> <a> </p>
我知道如何使用removeChild函数,但还没有找到使用addChild函数的文档和示例
如果我在php数组中有节点字符串,并且可以通过一个按钮通过GET调用执行包含该数组的php脚本,单击如何使用addChild函数将节点字符串放入div?
addChild
是一个php函数,javascript函数称为appendChild
:
var dv = document.createElement("p");
dv.innerHTML = "<a href=""> </a> ";
document.getElementById('test').appendChild(dv)
var dv=document.createElement(“p”);
dv.innerHTML=“”;
document.getElementById('test').appendChild(dv)
或者,您可以将节点字符串附加到html:
document.getElementById('test').innerHTML+='
“
W3Schools提供了使用的示例,因为您拥有的是一个字符串,所以首先需要创建它的DOM表示(您不能将字符串传递给appendChild,只能传递DOM元素)。要做到这一点,最简单的方法是将其放入另一个元素中,然后获取其内容
var temp = document.createElement('div'); // create a temporary dom element
temp.innerHTML = '<p><a href="#">some cool html string</a></p>'; // give it your html string as content, the browser will then create the corresponding dom representation
var childs = temp.childNodes; // grab the resulting child elements
document.getElementById('where_to_append').appendChild(childs); // append them to where you want
var temp=document.createElement('div');//创建一个临时dom元素
temp.innerHTML='';//将html字符串作为内容提供给它,然后浏览器将创建相应的dom表示
var childs=temp.childNodes;//获取生成的子元素
document.getElementById('where_to_append').appendChild(childs);//将它们附加到您想要的位置
或者,您也可以通过使用innerHTML简单地设置它,而不使用appendChild:
var my_div = document.getElementById('where_to_append');
my_div.innerHTML = my_div.innerHTML + '<p><a href="#">some cool html string</a></p>';
var my_div=document.getElementById('where_to_append');
my_div.innerHTML=my_div.innerHTML+'';
您可以使用append()
方法来完成此操作。下面是一个示例,我定义了新的\u子项
,并将其添加到父项div中
var new_child = jQuery(document.createElement('p')).html("<p> <a href=""> <a></p>");
$('parent-div').append(new_child);
var new\u child=jQuery(document.createElement('p')).html(“”);
$('parent-div')。追加(新的子项);
如果您使用php获取节点字符串,为什么不直接内联回显它呢?此链接可能非常有用。这肯定是一个php问题。口头解释很有用