이것이 제가 따라야 할 지시입니다. 검색 결과로 무엇을해야하는지 알려주지 않았으므로 결과를 표시하기 위해 exec () 메서드를 사용하기로 결정했습니다.
내 코드에 결과가 표시되지 않습니다.
html :
<div id="string_search">
<h3>Requirement #1</h3>
<p>Enter a string in the first box. <br>
Enter a search term in the second box.<br>
Click the button to find the term.</p>
<textarea id="user_string" rows="4" cols="50" placeholder="Enter string (text) here."></textarea>
<form>
Enter Search Term:<input type="text" id="search_term" size="45"><br>
<input type="button" id="search_button" value="Search" onclick="SearchString()"><br>
</form>
<textarea id="search_results" placeholder="Results here..." rows="4" cols="30"></textarea>
</div>
자바 스크립트 :
function SearchString(){
var user_string = document.getElementById("user_string").innerHTML;
var search_term = document.getElementById("search_term").value;
// regex to find all instances of search term
var re = new RegExp(search_term,"gi");
//use exec() to return matched text
var result = re.exec(user_string);
//display results in textarea "search_results"
document.getElementById("search_results").innerHTML = result;
}
콘솔 오류가 발생하지 않습니다. 코드가 작동하지 않습니까? 맞춤법 오류가 보이지 않거나 유사하지 않을 수 있습니다. 잠시 이걸 쳐다 봤는데
잘 작동합니다. 몇 가지 실수가있었습니다.
function SearchString(){
var user_string = document.getElementById("user_string").value;
var search_term = document.getElementById("search_term").value;
// regex to find all instances of search term
var re = new RegExp(search_term,"gi");
//use exec() to return matched text
var result = re.exec(user_string);
//display results in textarea "search_results"
document.getElementById("search_results").innerHTML = result;
}
사용 value
하지 않아야하는 입력 요소innerHTML
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다