Ver Mensaje Individual
  #16 (permalink)  
Antiguo 18/11/2010, 14:01
Avatar de IsaBelM
IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Donde encuentro un tutorial para esto:

no suelo hacer esto. pero como veo que has trabajado, esta es mi propuesta para solventar todos los problemas que explicabas en post anteriores
Cita:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>index</title>


<script type="text/javascript">
window.onload = function() {
var pElements = document.getElementsByTagName('p');
for (var i=0; i < pElements.length; i++) {
if(pElements[i].className == 'editable')
pElements[i].onclick = function() {inPlaceEditor(this);};
};
};


function inPlaceEditor (editableElement) {

//Getting the text from the p-element
var text = editableElement.innerHTML;

var padre = document.createElement('p');

// create and style the textarea element
var textarea = document.createElement('textarea');
textarea.setAttribute('cols', '10');
textarea.setAttribute('rows', '5');
textarea.style.width = "100%";

// Create the text for the textarea (the same text saved from p-elemnt)
var nyText = document.createTextNode(text);

// Put all together
textarea.appendChild(nyText);

padre.appendChild(textarea);

editableElement.parentNode.replaceChild(padre, editableElement);

//Creating and styling the buttons
var editbutton = document.createElement('input');
editbutton.setAttribute('type', 'button');
editbutton.setAttribute('value', 'Editar');
editbutton.onclick = function() {edit(this.parentNode, this.parentNode.firstChild.value);};

var cancelbutton = document.createElement('input');
cancelbutton.setAttribute('type', 'button');
cancelbutton.setAttribute('value', 'Cancelar');
cancelbutton.onclick = function() {cancel(this.parentNode, text);};



//Setting the buttons on the page
textarea.parentNode.insertBefore(cancelbutton, textarea.nextSibling);
textarea.parentNode.insertBefore(editbutton, textarea.nextSibling);
};



function cancel(parrafo, texto){
var pElement = document.createElement('p');
pElement.setAttribute('class', 'editable');
pElement.onclick= function() {inPlaceEditor(this);};
var original = document.createTextNode(texto);
pElement.appendChild(original);

parrafo.parentNode.replaceChild(pElement, parrafo);
};




function edit(parrafo, texto){
var pElement = document.createElement('p');
pElement.setAttribute('class', 'editable');
pElement.onclick= function() {inPlaceEditor(this);};
var original = document.createTextNode(texto);
pElement.appendChild(original);

parrafo.parentNode.replaceChild(pElement, parrafo);
};
</script>


</head>

<body>
<div id="content">
<h1>Los siguientes parrafos se pueden editar</h1>
<p class="editable">Texto editable</p>
<p class="editable">Texto editable</p>

<h2>Los siguientes parrafos no se pueden editar</h2>
<p class="not_editable">Texto no editable</p>
</div>
</body>
</html>
como puedes ver hay algunas modificaciones. una de ellas es para no tener que usar indices. para ello lo que he hecho, ha sido insertar todo en un <p> de esa manera el textarea y los botones son hijos de ese <p> y tener acceso al cualquiera de ellos sin necesidad de indices