如何使用if else显示文本

时间:2014-03-22 07:16:34

标签: javascript if-statement text

所以,现在我输入这个。最初,我正在寻找iframe目标,但我已经有了目标。我想做的是当我点击图像时,描述将出现在第二个td iframe上。如何将功能链接到iframe(第1个)

<script>
function myFunction(){
   var resultBox = document.getElementById("scarf1");
   if(resultBox == "scarf1.jpg"){  // <= you can put your condition here
       resultBox.innerHTML="A soft brushed cashmere scarf featuring the iconic check. The scarf is made in Scotland at a mill with a long heritage in producing cashmere.To create a subtle lustre and soft texture, the cashmere is washed in spring water, then woven on traditional looms and brushed with natural teasels.Measuring 168 x 30cm/66.1 x 11.8in, the design is hand-finished with fringing.";
   }</script>
    <table>
<tr>

    <td width="190px" height="190px">
    <a href="scarf1.jpg" target="frame"><img alt="HERITAGE CHECK CASHMERE SCARF" onclick="myFunction()" id="scarf1"height="100" src="scarf1.jpg" width="100"   onmouseover="bigImg(this)" onmouseout="normalImg(this)"/></a>
    </td>
    <td>
    <iframe src="Burberry_product_women_accessories_frame2.html" target="frame1" name="frame1" scrolling="no">
    </iframe></td>
    <td rowspan="4">

    <iframe height="750" width="480" target="frame" name="frame" scrolling="no" float="right" src="Burberry_product_women_accessories_frame.html">
    </iframe>
    </td>
    </tr>
    <tr>
    <td width="190px" height="190px">
    <a href="scarf2.jpg" target="frame"><img alt="HERITAGE CASHMERE SCARF" height="100" src="scarf2.jpg" width="100"  onmouseover="bigImg2(this)" onmouseout="normalImg2(this)" /></a>
    </td>
    <td>HERITAGE CASHMERE SCARF</td>
    </tr>
    <tr>
    <td width="190px" height="190px">
    <a href="wwallet.jpg" target="frame"><img alt="HAYMARKET CHECK CONTINENTAL WALLET" height="100" src="wwallet.jpg" width="100"   onmouseover="bigImg3(this)" onmouseout="normalImg3(this)"/></a>
    </td>
    <td>HAYMARKET CHECK CONTINENTAL WALLET</td>
    </tr>
    <tr>
    <td width="190px" height="190px">
    <a href="wumbrella.jpg" target="frame"><img alt="CHECK FOLDING UMBRELLA" height="100" src="wumbrella.jpg" width="100"   onmouseover="bigImg4(this)" onmouseout="normalImg4(this)"/></a>
    </td>
    <td>CHECK FOLDING UMBRELLA</td>
    </tr>

    </table>

4 个答案:

答案 0 :(得分:1)

function myFunction(){
   var resultBox = document.getElementById("demo");
   if(textA){  // <= you can put your condition here
       resultBox.innerHTML=textA;
   }else{
       resultBox.innerHTML=textB; 
   }
  // OR you can use ternary operator
  // resultBox.innerHTML =  (textA) ? textA : textB;
}

<强>更新

很难知道你想要做什么,但是当你找到特定的图像时,你似乎想要放置html .. 的 HTML

<img alt="HERITAGE CHECK CASHMERE SCARF" onclick="myFunction('scarf1.jpg')"

<强> JS

 function myFunction(imgVal){
   var resultBox = document.getElementById("resultBox"); 
   // here is the id of result element instead ---^--- of resultBox
   if(imgVal == "scarf1.jpg"){  // <= you can put your condition here
       resultBox.innerHTML="A soft brushed cashmere....... ";
    }
} 

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
    <head>
        <script>
            function myFunction(text) {
                document.getElementById("demo").innerHTML = text;
            }
        </script>
    </head>
    <body>
        <p>Click the button to trigger a function.</p>
        <button onclick="myFunction(/* What you need to check */ ? 'Text 1' : 'Text 2')">Click me</button>
        <p id="demo"></p>
    </body>
</html>

答案 2 :(得分:0)

你走了: 的 JS:

function myFunction()
{
i=true; // i is true
if (i) // Check if i is true.
{
document.getElementById("demo").innerHTML="i is true"; // Event to do if i is true
}
else // else if i is not the condition above (true)
{
document.getElementById("demo").innerHTML="i is false"; 
/* 
*Event to do if i is not the condition above
*/
}
}

答案 3 :(得分:0)

这是你可以测试的东西: 不是最好的代码,但你可以将它粘贴到w3schools编辑器中,你应该能够看到它的实际效果。

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
if(document.getElementById("demo").innerHTML == "Hello World") {
document.getElementById("demo").innerHTML="Click Me";
} else {
document.getElementById("demo").innerHTML="Hello World";
}
}
</script>
</head>
<body>

<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

</body>
</html>