例如,我有不同的id,如temp1,temp2,temp3等,用于唯一标识的不同按钮 他们。
是否可以将Id从一个页面(页面1)发送到另一个页面2,其中(第2页)我定义了以下内容 代码,考虑到第1页上的用户点击了带有temp1的按钮。
<script>
var button = $.getUrlVars()['button'];
$("#temp1").click(function() {
$("#sTinyMceEditorId").load("Folder/firsttemplate.html");
});
</script>
<script>
$("#temp2").click(function() {
$("#sTinyMceEditorId").load("Folder/secondtemplate.html");
});
</script>
<script>
$("#temp3").click(function() {
$("#sTinyMceEditorId").load("Folder/thirdtemplate.html");
});
</script>
上面的代码是在第2页上定义的。所以,我想如果我能以某种方式将特定的id转移到定义了上面代码片段的地方,那么将启动相应的脚本标记和相应的模板将被加载到TinyMCeEditor(id为sTinyMceEditorId)
我想知道这种方法是否有效?另外,上面的方法是为每个临时ID的好处定义单独的脚本标记吗?
由于
有助于回答的一些信息:
我正在使用的TinyMce版本是4.0.8这是我正在研究和在Sessions内部的ColdFusion 9。也许这与我上面的问题无关,因为我认为可以使用jQuery和java脚本完成所有的事情。
答案 0 :(得分:1)
您可以通过get参数将按钮id传递到下一页,然后使用jQuery读取它。
在第1页格式化您的链接
<a href="page2.html?button=temp1">Button 1</a>
第2页
var button = $.getUrlVars()['button'];
将值'temp1'存储到变量按钮
中或类似的东西。
修改强> 我在下面为你编写了代码,但是你想要了解你的JavaScript。
第2页
<script type="text/javascript">
var button = $.getUrlVars()['button'];
//these values should correspond to the values you are passing in your get parameters.
switch(button){
case 'temp1':
$("#sTinyMceEditorId").load("Folder/firsttemplate.html");
break;
case 'temp2':
$("#sTinyMceEditorId").load("Folder/secondtemplate.html");
break;
case 'temp3':
$("#sTinyMceEditorId").load("Folder/thirdtemplate.html");
break;
default:
alert("Button id not recognized.");
break;
}
</script>
编辑: getUrlVars函数作为jQuery的扩展 - 忘了包含。
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++){
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
})