这是我的HTML和jQuery片段:
<div id="navigator">
<div class="breadcrumb">
<a href="#">Competitors</a>
</div>
<div class="video-browser">
<div class="folder">
<a id="221" class="accessor">
<img src="/assets/images/folder.png" />
<span>Aron#1</span>
</a>
</div>
<script type="text/javascript">
$("a.accessor").click(function(event) {
var cId = this.id;
$.get("/video/" + cId)
.done(function(data){
$("#navigator").html(data);
})
.fail(function(data) {
alert("An error occured during request. Please try again later!");
});
});
</script>
</div>
</div>
如您所见,当响应到来时,jquery 替换了HTML内容,但它也用其他一些脚本替换了自身(这是来自jQuery请求作为响应的内容)。所以首先我在视图页面中得到这个。问题是当我查看HTML源代码时,HTML和脚本没有改变。所以你明白,当我进一步点击时,我会得到一些奇怪的行为。
谁能告诉我我所做的是否好,如果是,我的脚本可能有什么问题?
谢谢。
编辑1:
替换当前HTML内容的响应是:
<div class="breadcrumb">
<a href="/video">Competitors</a>
<span>></span>
<a href="#">Aron#1</a>
</div>
<div class="video-browser">
<div class="folder">
<a id="261_241" class="accessor">
<img src="/assets/images/movie_file.png" />
<span>test</span>
</a>
</div>
<script type="text/javascript">
$("a.accessor").click(function(event) {
var compId = this.id.substring(0, this.id.indexOf("_"));
var movieId = this.id.substring(this.id.indexOf("_") + 1);
$.get("/video/" + compId + "/" + movieId)
.done(function(data){
$("#videoPlayerDiv").html(data).html();
})
.fail(function(data) {
alert("An error occured during request. Please try again later!");
});
});
</script>
</div>