페이지가로드 될 때마다 임의 캡션을로드하려고합니다. 별도의 텍스트 파일이 있고 각 줄에 문자열이 들어 있습니다. 나는 HTML과 Javascript에 익숙하지 않다.Javascript를 사용하여 텍스트 파일에서 임의 캡션로드 및
HTML :
<div class="centerpiece">
<h1>DEL NORTE BANQUEST</h1>
<p class="caption"><script src = "js/caption.js"></script><script>getCaption();</script></p>
<a class="btn" id="browse-videos-button" href="#video-list">Browse Videos<br><img src="img/arrow-down.svg"style="width:15px;height:15px;"></a>
</div>
자바 스크립트 : 소스 파일에서
이function getCaption()
{
var txtFile = "text/captions.txt"
var file = new File(txtFile);
file.open("r"); // open file with read access
var str = "";
var numLines = 0; //to get the range of lines in the file
while (!file.eof)
{
// read each line of text
numLines += 1;
}
file.close();
file.open("r");
var selectLine = Math.getRandomInt(0,numLines);//get the correct line number
var currentLine = 0;
while(selectLine != currentLine)
{
currentLine += 1;
}
if(selectLine = currentLine)
{
str = file.readln();
}
file.close();
return str;
}
텍스트 :
We talked yesterday
Freshman boys!
5/10
I'm having a heart attack *pounds chest super hard
이 사이트는 텍스트 파일이 혼란했다 경우 내 고등학교 크로스 컨트리 팀이다 .
대부분의 구문에 익숙하지 않아 어떻게 든 다시 설정해야하는 경우 루프를 통해 파일을 반복하여 볼 수 없으므로 파일을 두 번 닫고 두 번 닫았습니다. 다음은 변경하려고하는 특정 캡션의 jsfiddle과 자바에서의 내 기능입니다.
https://jsfiddle.net/7cre9qqj/당신은 내가 알고 당신이 가질 수있는 비판이 엉망처럼 보이는 경우 주저하지 말아 주시기 바랍니다 주시기 바랍니다 작동하도록 더 많은 코드가 필요한 경우
, 나는 결국 배우려고 노력하고 있어요! 도와 줘서 고마워!
@EvanJameson이 방법이 작동하는지 또는 더 자세한 정보가 필요하면 알려주십시오 – dave