javascript - 如何使用 if else 显示文本

标签 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>

最佳答案

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....... ";
    }
} 

关于javascript - 如何使用 if else 显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22574656/

相关文章:

regex - 在R中匹配多个正则表达式

c# - 剪切文本以适合文本框宽度,然后右对齐

javascript - 更新 redux reducer 状态

javascript - li clicked 函数使用 jquery

javascript - JSON 文件问题中的 knockout 绑定(bind)

javascript - 导航和向右浮动的问题

c# - 避免过多的 if else, else if 语句用于多个 (9) 条件

c - 我无法理解 for 循环 block 和 if 语句

python - 如何在操作前检查 xpath 是否可用?

CSS 下拉菜单格式问题