자바 스크립트를 사용하여 하나의 파일을 업로드했습니다. 텍스트 파일의 일부를 클릭 가능하게 강조 표시하고 싶습니다. 예 : 업로드 된 파일의 모든 "안녕하세요"를 클릭 가능하게 강조 표시하고 싶습니다.업로드 된 텍스트 파일의 일부분을 클릭하여 자바 스크립트를 사용하여 만듭니다.
버튼 태그를 사용하고 CSS의 배경 및 테두리 속성을 변경했기 때문에 텍스트를 강조 표시 할 수 있지만 버튼을 클릭하면 onclick 동작을 수행 할 수 없습니다.
var sel_data=$("#sel").text(); // for taking the text file in avariable
var str='hello';
//making the regular expression of str
var re = new RegExp(str,"g");
//For replacing the 'str' by highlighted and clickable 'str'
var re_str="<button class='highlight' id='ty' onclick='alertfunc()' value="+str+">"+str+"</button>"
//replacement of 'str' by highlighted and clickable 'str'
var rep_data=sel_data.replace(re,re_str);
sel_data=rep_data;
//function to be executed when the button will get clicked
function alertfunc() {
alert('yellow');
}
나는이
var str='hello'
var re_str="<button class='highlight' id='ty' value="+str+">"+str+"</button>"
$(".highlight").click(function(){
alert("yellow");
})
처럼 또는 그들 중 누구도 작동하지 않습니다이
var button = document.getElementById("ty");
button.onclick = function(){
alert("yellow");
}
그러나처럼 그것을 시도, 을 제안하십시오 :
나는 이런 식으로 시도 위의 예를이 링크로 언급했습니다. Html make text clickable without making it a hyperlink
어떻게 "텍스트 파일"을 출력합니까? 're_str' 변수로 무엇을합니까? 한 단어의 단어를 버튼으로 대체하려는 부분은 어디입니까? 귀하의 질문이 불완전하거나 명확하지 않습니다. – Brewal
내가 언급 한 모든 것을 편집하고 정리했습니다. – Learner