用jquery替换文本

时间:2010-07-07 17:15:29

标签: javascript jquery

所以我使用jquery来搜索和替换我的html页面中的某些文本。这是:

function offon(){
  $("#sidebar li").each(function(){
     $(this).html($(this).html().replace(/Off Premise/, "Liquor Store"));
     $(this).html($(this).html().replace(/On Premise/, "Bar/Restaurant"));
  });
}

这是createmarker函数,它使用第3部分工具提示div是包含所有html的“simple_example_window”。我尝试使用simple_example_window作为offon函数中的div,但它没有做任何事情。

http://gmaps-utility-library-dev.googlecode.com/svn/trunk/extinfowindow/docs/examples.html 是插件。

function createMarker(point, name, address, type) {
var marker = new GMarker(point, customIcons[type]);
  markerGroups[type].push(marker);
var html = '<span class="name"><b>' + name + '</b></span> <br/>' + address + '<br/>' +     type;
GEvent.addListener(marker, 'click', function() {
    marker.openExtInfoWindow(
      map,
      "simple_example_window",
     html,
      {beakOffset: 2}
    );

它就像一个魅力。现在唯一的问题是我在谷歌地图中的工具提示没有改变。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

在将数据附加到Google地图之前,您需要执行相同的文本替换。或者,当您将数据附加到谷歌地图时。

答案 1 :(得分:1)

我从评论中看出你的想法,这里只是一个优化版本:

function offon(){
  $("#sidebar li").html(function(i, h){
     return h.replace(/Off Premise/, "Liquor Store")
             .replace(/On Premise/, "Bar/Restaurant");
  });
}

You can test it here

.html()可以使用一个函数,并且不需要在此过程中创建不必要的jQuery对象(和.html()调用),这将导致 lot 节省了CPU周期。

答案 2 :(得分:0)

我们可能需要查看更多标记以确保选择器正确。同样,正如Boushley所提到的,时间可能是在jQuery执行后生成(或添加)工具提示。