JavaScript - Inclusión de sentencia if javascript

 
Vista:
sin imagen de perfil

Inclusión de sentencia if javascript

Publicado por Juan Pablo (2 intervenciones) el 08/09/2016 19:04:47
Hola a todos.

Estoy buscando ayuda en este tema, de incorporar un código javascript para que muestre imágenes desde una sentencia if.

Ejemplo: si "log" es: sacaste los perros? Muestre la imagen de dos perros.
si "log" es: SI. Muestre: otra imagen diferente.

log es el resultado de la selección del nodo que se encuentra en

código html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html class="js-available">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="content-language" content="es">
<meta name="viewport" content="width=1000">
 
 
 
 
 
 
<head>
 
 
 
<!-- JIT Library File -->
<script language="javascript" type="text/javascript" src="jit.js"></script>
 
<!-- Example File -->
<script language="javascript" type="text/javascript" src="prueba.js"></script>
</head>
 
 
 
 
<body onload="init();">
 
<div id="container">
<div id="center-container">
<div id="infovis"></div>
 
</div>
<div id="log"></div>
<div id="dat"></div>
</div>
<footer>
</footer>
</body>
</html>

código prueba.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var labelType, useGradients, nativeTextSupport, animate;
 
(function() {
var ua = navigator.userAgent,
iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
typeOfCanvas = typeof HTMLCanvasElement,
nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
textSupport = nativeCanvasSupport
&& (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
//I'm setting this based on the fact that ExCanvas provides text support for IE
//and that as of today iPhone/iPad current text support is lame
labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
nativeTextSupport = labelType == 'Native';
useGradients = nativeCanvasSupport;
animate = !(iStuff || !nativeCanvasSupport);
})();

var Log = {
elem: false,
write: function(text){
if (!this.elem) 
/* this.elem = document.getElementById('dat'); */
this.elem = document.getElementById('log');
this.elem.innerHTML = text;
this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';


}
};

/*var Log = {
elem: false,
write: function(text){
if (!this.elem) 
this.elem = document.getElementById('log');
this.elem.innerHTML = text;
this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
}
}; */


function init(){
//init data
var json = {
id: "node1",
name: "¿SACASTE LOS PERROS?",
data: {},
children: [{
id: "node2",
name: "NO",
data: {},
children: [{
id: "node4",
name: "SACA LOS PERROS",
data: {},
children: []
}]
}, {
id: "node3",
name: "SI",
data: {},
children: [{
id: "node5",
name: "TUS PERROS TE LO AGRADECEN",
data: {},
children: []
}]
}]
};
//end
//init Spacetree
//Create a new ST instance
var st = new $jit.ST({
//id of viz container element
injectInto: 'infovis',
//set duration for the animation
duration: 300,
//set animation transition type
transition: $jit.Trans.Quart.easeInOut,
//set distance between node and its children
levelDistance: 50,
//enable panning
Navigation: {
enable:true,
panning:true
},
//set node and edge styles
//set overridable=true for styling individual
//nodes or edges
Node: {
height: 67,
width: 120,
type: 'rectangle',
color: '#aaa',
overridable: true
},
 
Nodes: {
height: 150,
width: 120,
type: 'rectangle',
color: '#aaa',
overridable: true
},
 
Edge: {
type: 'bezier',
overridable: true
},
 
 
 
onBeforeCompute: function(node){
 
 
Log.write(node.name);
 
 
 
 
},
 
 
 
 
/*	onAfterCompute: function(){
Log.write("continue ");
}, */
 
 
 
//This method is called on DOM label creation.
//Use this method to add event handlers and styles to
//your node.
onCreateLabel: function(label, node){
label.id = node.id;
label.innerHTML = node.name;
label.onclick = function(){
if(normal.checked) {
st.onClick(node.id);
} else {
st.setRoot(node.id, 'animate');
}
};
//set label styles
var style = label.style;
style.width = 120 + 'px';
style.height = 50 + 'px';
style.cursor = 'pointer';
style.color = '#333';
style.fontSize = '0.8em';
style.textAlign= 'center';
style.paddingTop = '1px';
},
 
//This method is called right before plotting
//a node. It's useful for changing an individual node
//style properties before plotting it.
//The data properties prefixed with a dollar
//sign will override the global node style properties.
onBeforePlotNode: function(node){
//add some color to the nodes in the path between the
//root node and the selected node.
if (node.selected) {
node.data.$color = "#ff7";
}
else {
delete node.data.$color;
//if the node belongs to the last plotted level
if(!node.anySubnode("exist")) {
//count children number
var count = 0;
node.eachSubnode(function(n) { count++; });
//assign a node color based on
//how many children it has
node.data.$color = ['#aaa', '#baa', '#caa', '#daa', '#eaa', '#faa'][count]; 
}
}
},
 
//This method is called right before plotting
//an edge. It's useful for changing an individual edge
//style properties before plotting it.
//Edge data proprties prefixed with a dollar sign will
//override the Edge global style properties.
onBeforePlotLine: function(adj){
if (adj.nodeFrom.selected && adj.nodeTo.selected) {
adj.data.$color = "#eed";
adj.data.$lineWidth = 3;
}
else {
delete adj.data.$color;
delete adj.data.$lineWidth;
}
}
});
//load json data
st.loadJSON(json);
//compute node positions and layout
st.compute();
//optional: make a translation of the tree
st.geom.translate(new $jit.Complex(-200, 0), "current");
//emulate a click on the root node.
st.onClick(st.root);
//end
//Add event handlers to switch spacetree orientation.
var top = $jit.id('r-top'),
left = $jit.id('r-left'),
bottom = $jit.id('r-bottom'),
right = $jit.id('r-right'),
normal = $jit.id('s-normal');


function changeHandler() {
if(this.checked) {
top.disabled = bottom.disabled = right.disabled = left.disabled = true;
st.switchPosition(this.value, "animate", {
onComplete: function(){
top.disabled = bottom.disabled = right.disabled = left.disabled = false;
}
});
}
};

top.onchange = left.onchange = bottom.onchange = right.onchange = changeHandler;
//end

}

saludos.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Inclusión de sentencia if javascript

Publicado por xve (2100 intervenciones) el 09/09/2016 08:38:18
Hola Juan Pablo, tienes el código sin ninguna tabulación... y esto hace muy difícil revisar-lo para poder ayudarte!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar