好的,所以我一直在努力解决这个问题,现在想我需要一点帮助。
我有两个段落,id 的 show1 和 show2。
我有一个简短的文本,每个文本都应该在单击它们上方的链接时展开。
我的函数可以展开和折叠,但感觉它没有获得链接的值,更准确地说,是链接文本,因为我得到“链接为空”。
它一直有效到 var status 和 innerHTML,如果我注释掉它有效的这两行,那么它不会将我的链接文本从显示更改为隐藏...所以如果有任何善良的灵魂可以帮助我,那就太好了赞赏。
/* Function created by "Simon Willson" to be able to
call several functions with a single event */
//Create the function
function addLoadEvent(func) {
//Create a variable for window.onload event
var oldonload = window.onload;
//If window.onload is NOT a function, then assign 'func' to window.onload
if (typeof window.onload != 'function') {
window.onload = func;
//If window.onload already is a function then make a new function
} else {
window.onload = function() {
//To do what the old onload function did
if (oldonload) {
oldonload();
}
//then do whatever the new function does
func();
}
}
}
function newLink() {
//Make a few safety check to see if the browser can handle the elements
if (!document.getElementById) {
if (!document.createElememt) {
if (!document.createTextNode) {
return false;
}
}
}
//Create the link
newLinkElement = document.createElement('a');
//Give the link a Id
newLinkElement.id = 'show1_link';
//Set the href
newLinkElement.href = "javascript:showHide(this.id,'show1')";
//Create a variable for the link text
var linkText = document.createTextNode('Visa mera information');
//Append the text to the link
newLinkElement.appendChild(linkText);
//Create a variable for the paragraph
var elem = document.getElementById('show1')
//Insert the text before the paragraph with the Id show1
elem.parentNode.insertBefore(newLinkElement,show1);
}
addLoadEvent(newLink);
function showHide(link_id,elemId) {
var link = document.getElementById(link_id);
var text = document.getElementById(elemId);
text.style.display = (text.style.display == 'block') ? 'none' : 'block';
var status = (text.style.display == 'block') ? 'none' : 'block';
text.style.display = status;
link.innerHTML = (status == 'block') ? 'Dölj information' : 'Visa mera information';
}