2016-01-08 32 views
0

您好我已經創建了一個HTML和JavaScript的網站,在那裏,如果你打個招呼它回覆的東西回來條件語句添加圖片,而不是文字。我怎樣才能在if或else語句

function myFunction() { 
 
    var letter = document.getElementById("myInput").value.toLowerCase(), 
 
     text; 
 

 
    if (letter === "hi im jack") { 
 
     text = "Spot on! Good job!"; 
 
    } else if (letter === "hi" || letter === "hey" || letter === "hello" || letter === "hi there") { 
 
     text = "Hi I Am Mike"; 
 
    } 
 
    document.getElementById("demo").innerHTML = text; 
 
}
<input id="myInput" type="text" onkeyup="myFunction()" style="font-size: 30pt; height: 50px; width: 600px;"> 
 
<button onclick="myFunction()" style="font-size: 30pt; height: 50px; width: 200px;">ENTER</button> 
 
<p id="demo">result here</p>

的問題是我需要添加圖片代替文字的答覆之一。我還需要知道如何使它自動發送給其他網站。我正在尋找一些非常簡單的東西,如果可能的話,它可以替換「text ==」。

+0

您需要使用img元素中innerHTML語句,即document.getElementById(「demo」)。innerHTML =「my picture jeff

回答

1

使用,你使用和分配新值文本變量,如下所示相同的代碼:

function myFunction() { 
var letter = document.getElementById("myInput").value.toLowerCase(), 
    text; 

if (letter === "hi im jack") { 
    text = '<img src="imageName.jpg" width="200" height="200" />'; //Show an image 
} else if (letter === "hi" || letter === "hey" || letter === "hello" || letter === "hi there") { 
    location.href = 'http://www.websitename.com'; //Redirect automatically 
} 
document.getElementById("demo").innerHTML = text; 

}

+0

什麼是'href ='http:// www.websitename.com''應該做的? –

+0

謝謝@Wessam El Mahdy。我怎麼會讓這個沒有自動鏈接。謝謝。 – Jack

+0

@LeeTaylor對不起,您應該使用location.href ='http://www.websitename.com'; location.href應該將當前網頁位置更改爲另一個位置,我更新了代碼。 –

1

請參閱這個答案,它可能會幫助:How to display image with javascript?

廣場下面,而不是文本行,它應該工作。

text = ('< img src="+http:xxx+"/>'); 
+0

虐待稍後@Maihan Nijat thanks – Jack

0

你可以使用switch語句,而不是if switch語句是一種像一個if else條件與一些語法糖如

switch(chackVariable){ 
    case value: 
    doSomeStuff() 
    break; // break will end the switch statement and prevent testing any other cases 
} 

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/switch

然後另一部分問題與創建圖像有關,我只是使用innerHTML將其創建爲字符串,如前所述。

function myFunction() { 
 
    // try to save your helper variables at the top of the function 
 
    var 
 
    input = document.getElementById("myInput"), 
 
    demo = document.getElementById("demo"), 
 
    letter = input.value.toLowerCase(), 
 
    text; 
 
    
 
    switch(letter){ 
 
    case 'hi im jack': 
 
     text = 'Spot on! Good Job!'; 
 
     break; 
 
    // you can do && conditions by doing multiple cases for one set of functionality 
 
    case 'hi': 
 
    case 'hello': 
 
    case 'hi there': 
 
     text = 'Hi I Am Mike'; 
 
     break; 
 
    case 'image': 
 
     text = '<img src="http://lorempixel.com/100/100/" />'; 
 
     break; 
 
    case 'personal space': 
 
     // you can redirect the page with js by setting the value for document.location.href 
 
     // it wont work on stack however due to the same origin policy and settings on the other web page 
 
     document.location.href = 'https://www.youtube.com/embed/2DfmDuOxcN8'; 
 
     break; 
 
    // default will be run if none of the cases match or the previous cases did not have a break 
 
    default: 
 
     text = 'result here'; 
 
    } 
 
    
 
    demo.innerHTML = text; 
 
}
<input id="myInput" type="text" onkeyup="myFunction()" style="font-size:30pt;height:50px;width:600px;"> 
 

 
<button onclick="myFunction()" style="font-size:30pt;height:50px;width:200px;">ENTER</button> 
 

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