获取父节点的一部分';通过JavaScript访问内容

获取父节点的一部分';通过JavaScript访问内容,javascript,dom,Javascript,Dom,如何通过javaScript获取该锚的父对象 <p>Lorem Ipsum is simply dummy text of the printing and typesetting <a href="http://somplace.com/index.php?content= {{in this place i want to retrieve 20 character of the parent p}}"></a> industry. </p>

如何通过javaScript获取该锚的父对象

<p>Lorem Ipsum is simply dummy 
text of the printing and typesetting
<a href="http://somplace.com/index.php?content=
{{in this place i want to retrieve 20 character of 
the parent p}}"></a> industry.
</p>
Lorem Ipsum只是个傀儡 印刷和排版的文本 工业。


使用
this.parentNode.innerHTML.substring(0,20)


使用
this.parentNode.innerHTML.substring(0,20)


假设您已经在名为
anchoreElement
的变量中引用了锚定标记,您可以获得如下父级:

var anchorParent = anchorElement.parentNode;
或者,如果您试图获取对锚的引用,您可以给它一个id,然后这样做以实际获取文本并将其放入href:

<p>Lorem Ipsum is simply dummy 
text of the printing and typesetting
<a id="myAnchor" href="http://somplace.com/index.php?content=
{{in this place i want to retrieve 20 character of 
the parent p}}"></a> industry.
</p>

var elem = document.getElementById("myAnchor");  // get anchor object
var parent = elem.parentNode;                // get parent
var text = parent.innerHTML.substr(0,20);    // get first 20 chars of text
elem.href = "http://somplace.com/index.php?content=" + text;
Lorem Ipsum只是个傀儡 印刷和排版的文本 工业。

var elem=document.getElementById(“myAnchor”);//获取锚定对象 var parent=elem.parentNode;//得到父母 var text=parent.innerHTML.substr(0,20);//获取前20个字符的文本 elem.href=”http://somplace.com/index.php?content=“+案文;
假设您已经在名为
anchoreElement
的变量中引用了锚定标记,您可以获得如下父级:

var anchorParent = anchorElement.parentNode;
或者,如果您试图获取对锚的引用,您可以给它一个id,然后这样做以实际获取文本并将其放入href:

<p>Lorem Ipsum is simply dummy 
text of the printing and typesetting
<a id="myAnchor" href="http://somplace.com/index.php?content=
{{in this place i want to retrieve 20 character of 
the parent p}}"></a> industry.
</p>

var elem = document.getElementById("myAnchor");  // get anchor object
var parent = elem.parentNode;                // get parent
var text = parent.innerHTML.substr(0,20);    // get first 20 chars of text
elem.href = "http://somplace.com/index.php?content=" + text;
Lorem Ipsum只是个傀儡 印刷和排版的文本 工业。

var elem=document.getElementById(“myAnchor”);//获取锚定对象 var parent=elem.parentNode;//得到父母 var text=parent.innerHTML.substr(0,20);//获取前20个字符的文本 elem.href=”http://somplace.com/index.php?content=“+案文;

我建议您添加JavaScript的基本知识,如果您偶尔使用一次,可能不需要。

我建议您添加JavaScript的基本知识,如果您偶尔使用一次,可能不需要。

@bizzare:Updated,请检查。
innerText
可能不是跨浏览器的。是的,我在mozila中有一个错误`this.parentNode.innerText是未定义的`use InnerHTML用于跨浏览器支持。@Pranav:Yeah
innerText
不是跨浏览器解决方案,在facebook开发中你可以做
setText
或者innerText已经养成了这个习惯,试图避免:)@bizzare:Updated,请检查。
innerText
可能不是跨浏览器的。是的,我在mozila中有一个错误`this.parentNode.innerText未定义`use InnerHTML用于跨浏览器支持。@Pranav:Yeah
innerText
不是跨浏览器解决方案,在facebook开发中,你可以使用
setText
或innerText,你已经养成了这个习惯,试图避免:)为什么接受率这么低?我想这一次你已经找到了答案。因此,请将答案标记为已接受..简单Dyeah当然,我在等待时间20秒,提醒我检查接受复选标记!;)谢谢兄弟为什么接受率这么低?我想这次你已经得到了答案。因此,请将答案标记为已接受..简单Dyeah当然,我在等待时间20秒,提醒我检查接受复选标记!;)谢谢兄弟呵呵?这里面有什么更深的含义吗?这和这个问题有什么关系?这里面有什么更深的含义吗?它与这个问题有什么关系?