Prototype: .previous as an object?
开发者_如何学GoSo, here's the deal, I'm trying to use Element.previous to call an a previous element as an object... but it just gets the text of the element. How can I call on a previous object without knowing what it's id is? Calling on a previous element by class for example?
So, I figured it out partly: $('1').parentNode.previousSibling.firstChild calls the previous object.
Now, I'm having trouble getting it to call the previous object if an object has been deleted between them. Here's the script.
<script src="lib/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
function printBr(element, index, array) {
document.write("<div class=\"row\">");
document.write("<input type=\"text\" class=\"textfield\" onclick=\"$('a" + index + "').appear(); return false;\" id=\"" + index + "\" name=\"" + index + "\" value=\"" + element + "\"><br/>");
if (index == 0) {}
else {
document.write("<div id=\"a" + index + "\" style=\"display:none;width:150px;height:25px;background-color:blue;\">");
document.write("<a onmousedown=\"$('" + index + "').parentNode.previousSibling.firstChild.writeAttribute('value', $('" + index + "').parentNode.previousSibling.firstChild.value + $('" + index + "').value);$('" + index + "').remove();$('a" + index + "').hide(); return false;\"><< Move</a> | ");
document.write("<a onmousedown=\"$('a" + index + "').hide(); return false;\">X</a></div>");
}
document.write("</div>");
}
['hello, world','123','bye bye birdy'].forEach(printBr);
</script>
Use .previous with the CSS selector as a argument to .previous. Such as .previous('span'). Or this element you are looking for may not be truly the previous element. It may be that you need to go up() first.
精彩评论