0
爲什麼我的代碼不運行:搜索一個字 - 錯誤
function searchWord() {
var text = document.getElementById('some_text');
var word = document.getElementById('s_text');
var f_occurence = text.search(word);
var statement = "";
if (f_occurence != null) {
document.getElementById('text_d').innerHTML = "Not found the word in tex!";
} else {
document.getElementById('text_d').innerHTML = "It founded on position " + f_occurence;
}
}
}
<h1>
Search box
</h1>
<form>
<h4>Enter a word: </h4>
<input type="text" name="word" id="s_text">
<button onclick="searchWord()">Search</button>
</form>
<br>
<h3 id="text_d"></h3>
<br>
<br>
<br>
<hr/>
<p id="some_text" draggable="true">Sportsman do offending supported extremity breakfast by listening. Decisively advantages nor expression unpleasing she led met. Estate was tended ten boy nearer seemed. As so seeing latter he should thirty whence. Steepest speaking up attended it as.
Made neat an on be gave show snug tore. Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity.
Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me contained he pronounce do. Excellence inquietude assistance precaution any impression
man sufficient. Style too own civil out along. Perfectly offending attempted add arranging age gentleman concluded. Get who uncommonly our expression ten increasing considered occasional travelling. Ever read tell year give may men call its. Piqued
son turned fat income played end wicket. To do noisy downs round an happy books. Next his only boy meet the fat rose when. Do repair at we misery wanted remove remain income. Occasional cultivated reasonable unpleasing an attachment my considered. Having
ask and coming object seemed put did admire figure. Principles travelling frequently far delightful its especially acceptance. Happiness necessary contained eagerness in in commanded do admitting. Favourable continuing difficulty had her solicitude
far. Nor doubt off widow all death aware offer. We will up able in both do sing. Folly was these three and songs arose whose. Of in vicinity contempt together in possible branched. Assured company hastily looking garrets in oh. Most have love my gone
to this so. Discovered interested prosperous the our affronting insipidity day. Missed lovers way one vanity wishes nay but. Use shy seemed within twenty wished old few regret passed. Absolute one hastened mrs any sensible. However venture pursuit he
am mr cordial. Forming musical am hearing studied be luckily. Ourselves for determine attending how led gentleman sincerity. Valley afford uneasy joy she thrown though bed set. In me forming general prudent on country carried. Behaved an or suppose
justice. Seemed whence how son rather easily and change missed. Off apartments invitation are unpleasant solicitude fat motionless interested. Hardly suffer wisdom wishes valley as an. As friendship advantages resolution it alteration stimulated he
or increasing. Woody equal ask saw sir weeks aware decay. Entrance prospect removing we packages strictly is no smallest he. For hopes may chief get hours day rooms. Oh no turned behind polite piqued enough at. Forbade few through inquiry blushes you.
Cousin no itself eldest it in dinner latter missed no. Boisterous estimating interested collecting get conviction friendship say boy. Him mrs shy article smiling respect opinion excited. Welcomed humoured rejoiced peculiar to in an. Do to be agreeable
conveying oh assurance. Wicket longer admire do barton vanity itself do in it. Preferred to sportsmen it engrossed listening. Park gate sell they west hard for the. Abode stuff noisy manor blush yet the far. Up colonel so between removed so do. Years
use place decay sex worth drift age. Men lasting out end article express fortune demands own charmed. About are are money ask how seven.</p>
我嘗試創建一個字取景器。如果在文本中找到該單詞,我會在HTML頁面中打印開始時的位置。 這是我使用時的函數:var = str.search(word);
,並將此代碼放入函數中。但是當我搜索不工作! 謝謝!
您有一個額外的''}在函數結束時,將導致一個語法錯誤。 – Barmar
'search()'不返回'null'。如果找不到匹配項,則返回「-1」。 – Barmar