javascript Javascript获取标签中的字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4345093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript get string value in a tag
提问by QLiu
I have a question about how to read the string betweena tag, for example.
例如,我有一个关于如何读取标签之间的字符串的问题。
Devices connecting to HOME <a onclick="kmt_Toggle('BOX01', this)" class="cs_answer_link" value="[Show me how to download and install my map..]">[Show me how to download and install my map..]</a><br />
<br />
<div class="cs_answer_secondAccordion" id="BOX01" style="display: none;">
Steps for downloading and installing a map...
</div>
My code a simple, when users click on the a tag, the div class will show, and change Show me how to download and install map string in a tag to Close. When users click again, the current "close" will become "original long text". My main issue is that How i can read the string between a tag, thanks. I can not use JQuery
我的代码很简单,当用户点击 a 标签时,div 类会显示,并将 Show me how to download and install map string in a tag 更改为 Close。当用户再次点击时,当前的“关闭”将变成“原来的长文本”。我的主要问题是如何读取标签之间的字符串,谢谢。我不能使用 JQuery
function kmt_Toggle(obj, aTag) {
var el = document.getElementById(obj);
if ( el.style.display != 'none' ) {
el.style.display = 'none';
//var tagString=document.ge(aTag).valueOf();
// aTag.innerHTML='[more..]';
aTag.innerHTML= tagString;
}
else {
el.style.display = '';
aTag.innerHTML='[close..]';
}
}
Hello Guys, thanks.
大家好,谢谢。
I think i have not made my question crystal clear. I want to build a toggle function in this Javascript, if people clicks on a tag, for instance,
我想我没有把我的问题说得很清楚。我想在这个 Javascript 中构建一个切换功能,如果人们点击一个标签,例如,
<a onclick="kmt_Toggle('BOX01', this)" class="cs_answer_link" value="[Show me how to download and install my map..]">[Show me how to download and install my map..]</a>
It will show my div if my div has style = none; and change Show me how to download and install map into "close"
如果我的 div 有 style = none,它将显示我的 div;并将“显示我如何下载并安装地图”更改为“关闭”
下载和安装地图的步骤...If people click on the link again, it will go back to the "show me how to download and installl map" and hide my div.
如果人们再次单击该链接,它将返回“向我展示如何下载和安装地图”并隐藏我的 div。
I tried to build something by following idealmachine solution. It does not work.
我试图通过遵循 Idealmachine 解决方案来构建一些东西。这是行不通的。
Cheers, Qing
干杯,青
回答by ascanio
You can use:
您可以使用:
getElementsByTagName("tagName")[0].firstChild.nodeValue;
or:
或者:
getElementsById("id")[0].firstChild.nodeValue;
回答by PleaseStand
First of all, the valueattribute of an aelement is not officially standardized by the W3C. You don't need it.
首先,元素的value属性a并没有被 W3C 正式标准化。你不需要它。
That said, some lines of code that might work are:
也就是说,可能工作的一些代码行是:
// innerHTML way
aTag.originalInnerHTML = atag.innerHTML; // to save the innerHTML
aTag.innerHTML = '[close..]'; // to add new link text
aTag.innerHTML = aTag.originalInnerHTML; // to restore the innerHTML
// W3C DOM way
// Save
var cur = aTag.firstChild;
aTag.oldChildNodes = [];
while(cur) {
aTag.oldChildNodes.push(cur);
cur = cur.nextSibling;
}
// Add new link text
while(aTag.lastChild) aTag.removeChild(atag.lastChild);
aTag.appendChild(document.createTextNode('[close..]'));
// Restore
while(aTag.lastChild) aTag.removeChild(atag.lastChild);
for(var i = 0; i < aTag.oldChildNodes.length; ++i) {
aTag.appendChild(aTag.oldChildNodes[i]);
}
Or you could do either of these instead, similar to how you are switching the accordion sections on and off:
或者,您可以执行以下任一操作,类似于打开和关闭手风琴部分的方式:
- Insert both link texts into the HTML, then use CSS
display: none;to switch off the one you do not want. - Switch between entire
aelements using CSSdisplay: none;.
- 将两个链接文本插入 HTML,然后使用 CSS
display: none;关闭您不想要的链接文本。 a使用 CSS在整个元素之间切换display: none;。
回答by QLiu
I found out a way. I change my javascript like that
我找到了一个方法。我像那样改变我的javascript
function kmt_Toggle(obj, aTag) {
var el = document.getElementById(obj);
//Save original Store at the firs time.
if(!aTag.originalInnerHTML)
{
aTag.originalInnerHTML=aTag.innerHTML;
}
if (el.style.display != 'none' ) {
el.style.display = 'none';
aTag.innerHTML= aTag.originalInnerHTML;
}
else if( el.style.display =='none'){
el.style.display = '';
aTag.innerHTML='[close..]';
}
}"
//First I store aTag text string into a variables
//首先我将aTag文本字符串存储到一个变量中
回答by JohnGray
Maybe:
或许:
return document.getElementById(<your div name>).innerHtml;
回答by Breezer
you can use the
你可以使用
documentElement. getElementsByTagName( "div" )
you have do define after if you want the first element or second etc
如果您想要第一个元素或第二个等,您必须在之后定义
like so
像这样
documentElement. getElementsByTagName( "div" ).item(0).innerHtml;
will give you the innehtml of the first occurance of div
会给你第一次出现div的innehtml
or if you have an id
或者如果你有身
documentElement. getElementsById('idname')

