如何获取或记录具有当前焦点的元素的ID以便以后使用javascript进行检索 [英] How to Get or Record the ID of a element with the current focus for later retrieval using javascript
问题描述
我想要的是保存html值,然后在点击前面的按钮时显示它与上一个功能。
但是我忘了想想是 document.getElementById('id')
不返回值,它返回类似于 [object HTMLElement] 。
What i want is to save the html values and then display it with the previous function when the button called previous is clicked.
However what i forgot to think about is that document.getElementById('id')
doesn't return a value, it returns something like [object HTMLElement].
如何获取ID的值?
var namevalue1 = document.getElementById('name');
var imagetitlevalue1 = document.getElementById('imagetitle');
var locationvalue1=document.getElementById('location') ;
var similaritiesvalue1 = document.getElementById('similarities');
var typevalue1 = document.getElementById('type');
var costvalue1 = document.getElementById('cost');
var datevalue1 = document.getElementById('date');
var pictureidvalue1 = document.getElementById('pictureid');
function previous()
{
document.getElementById('name').innerHTML = namevalue1;
document.getElementById('location').innerHTML = locationvalue1;
document.getElementById('similarities').innerHTML = similaritiesvalue1;
document.getElementById('type').innerHTML = typevalue1;
document.getElementById('cost').innerHTML = costvalue1;
document.getElementById('date').innerHTML = datevalue1;
document.getElementById('pictureid').src = pictureidvalue1;
}
推荐答案
这取决于元素,如果您想要在元素中的HTML的文本表示,请使用 innerHTML
This depends on the type of element, if you want a textual representation of the HTML within the element, use innerHTML
document.getElementById("id").innerHTML
如果您只想要元素及其后代使用 textContent
If you only want the text within the element and its descendants use textContent
document.getElementById("id").textContent
最后,如果你使用< input>您可以使用的元素 value
Finally if you're working with <input> elements you could use value
document.getElementById("id").value
这篇关于如何获取或记录具有当前焦点的元素的ID以便以后使用javascript进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!