매우 큰 형식이있는 레지스터 뷰 파일이 있습니다. 따라서 jquery 폼 유효성 검사 플러그인과 함께 폼 마법사 플러그인을 사용할 계획입니다. 이 스크립트를 특정 페이지에만로드하고 싶습니다. 어떻게해야합니까? 여기 내 컨트롤러 방법입니다. codeigniter로드보기 특정 자바 스크립트
public function index(){
$data['title'] = "Register";
$this->load->view("site_header", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer");
}
는 I에 유래
here에 유사한 게시물을 보았다 그러나 나는 무엇을 이해하지 않습니다. 도와주세요.
[이 요지] (https://gist.github.com/qolami/5994689)가 도움이 될 수 있습니다. 이를 사용하여 외부 JavaScript 파일을 임베드하고 내부 JavaScript 문을 사용자 정의보기에 추가 할 수 있습니다. –
A
답변
9
변수로로드 할 j를 보낼 수 있습니다. 예제는 여기에 있습니다.
샘플 컨트롤러
class mypage extends CI_Controller{
public function index(){
$data['js_to_load']="index.js";
$this->load->view('header',$data);
$this->load->view('my_html_page');
}
public function second_page(){
$data['js_to_load']='second.js';
$this->load->view('header',$data);
$this->load->view('my_second_html_page');
}
}
header.php -보기 일이 뭐죠
<!-- Needed html code here -->
<? if ($js_to_load != '') : ?>
<script type="text/javascript" src="assets/js/<?=$js_to_load;?>">
<? endif;?>
<!-- Other html code here -->
파일?
js 파일을 $ data 변수로 설정하고 $ data 변수를 헤더로 전달합니다. 헤더 파일에서 js_to_load이 설정되어 있는지 확인하고 있습니까? 그렇다면 js 파일을 포함합니다.
또한 배열을 사용하여 다중 js 파일을 전달할 수 있습니다.
샘플 컨트롤러
class mypage extends CI_Controller{
public function index(){
$data['js_to_load']=array("index.js","index1.js","index2.js");
$this->load->view('header',$data);
$this->load->view('my_html_page');
}
public function second_page(){
$data['js_to_load']=array("second.js","second2.js","second2.js");
$this->load->view('header',$data);
$this->load->view('my_second_html_page');
}
}
header.php -보기 당신의 HTML로 바닥 글에해야 완전히 당신의 .js를로드하는 것이 좋습니다
<!-- Needed html code here -->
<? if (is_array($js_to_load)) : ?>
<? foreach ($js_to_load as $row):
<script type="text/javascript" src="assets/js/<?=$row;?>">
<?endforeach;?>
<? endif;?>
<!-- Other html code here -->
public function index()
{
$this->load->view('template/header');
$this->load->view('template/nav');
$this->load->view('thing/landing');
$this->load->view('template/footer');
}
내 바닥 글보기는 사용중인 컨트롤러의 어떤 컨트롤러 또는 방법 검출 $this->router->class; 또는 $this->router->fetch_method();를 사용하여
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$caller_class = $this->router->class;
$caller_method = $this->router->fetch_method();
?>
<script type="text/javascript" src="<?php echo base_url(); ?>public/js/jquery.min.js"></script>
<?php
//look for JS for the class/controller
$class_js = "public/js/custom-".$caller_class.".js";
if(file_exists(getcwd()."/".$class_js)){
?><script type="text/javascript" src="<?php echo base_url().$class_js; ?>"></script>
<?php
}
//look for JS for the class/controller/method
$class_method_js = "public/js/custom-".$caller_class."-".$caller_method.".js";
if(file_exists(getcwd()."/".$class_method_js)){
?><script type="text/javascript" src="<?php echo base_url().$class_method_js; ?>"></script>
<?php
}
?>
</body>
</html>
입니다. 그런 다음 컨트롤러/메소드에 대한 .js 파일이 있는지 확인하십시오.
작은 관리를 사용하면 바닥 글을 들여다 보지 않고 컨트롤러 및/또는 메소드 특정 스크립트를 사용할 수 있습니다.
파일 pub/js/custom-mycontrollername-mymethod.js에서 사용하면 (jquery); (JQuery와 권장으로) 문서가 ready이며 또한 DIV 페이지에 실제로 있는지 확인하기 위해 if ($("#mydivthing").length) {를 사용할 수있는 경우
[이 요지] (https://gist.github.com/qolami/5994689)가 도움이 될 수 있습니다. 이를 사용하여 외부 JavaScript 파일을 임베드하고 내부 JavaScript 문을 사용자 정의보기에 추가 할 수 있습니다. –