您的位置:首页 > Web前端 > CSS

jsp,css,js中变量的传递关系

2017-04-26 16:20 246 查看
一。从controller中传值到js页面,是不能像传到jsp页面那样直接接收,而要采用间接的传值:

1。由uil,请求: localhost:+'/bdmap?lat='+point.lat+'&lng='+point.lng;

2。controller中接收:

@GetMapping("bdmap")
public ModelAndView police(@RequestParam Map<String, Object> map ){
return new ModelAndView("mapBD/list").addObject("map", map); 
}

3。页面间接接收:

需要在html中引入:<html xmlns:th="http://www.thymeleaf.org">

<body>
<div   style=display:none;>

  <input id="map_lat" th:value="${map.lat}" /> 

  <input id="map_lng" th:value="${map.lng}" /> 
</div>

</body>

4。在js中获取控件的值:

var maplat = $("#map_lat").val();
var maplng = $("#map_lng").val();
if( maplat == "undefined" || maplng == "undefined"){
}

注意:在html中获取的值为空时,在js中取到的值时“undefined”,而不是undefined。

二,jsp中的js与加载的js文件中的js运行的先后次序与js文件加载的位置有关,在前的先运行。

三,jsp中变量与js文件中变量,若js中要用到jsp中文件的变量,则必须在js文件之前先运行并且要设为公用变量,反过来,如果jsp中要使用js中的文件变量,则要等到js文件加载完菜能使用。

如: 

 <script>

var plate = "<%=plate%>";

 </script> 
 <script type="text/javascript" src="<%=request.getContextPath()%>/js/appraise.js">

</script>

则,appraise需等上面代码运行完后再加载,所以apprraise.js中可使用plate变量。

 

 <script type="text/javascript" src="<%=request.getContextPath()%>/js/appraise.js">

</script>

<script>

var plate = "<%=plate%>";

 </script> 

则,appraise在上面代码运行之前就加载了,所以apprraise.js中不可使用plate变量。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: