2012-09-05 26 views
2

我想讓彈出式插件在Javascript字符串內工作。所以當用戶鼠標懸停geolocation時,彈出窗口就會出現,並且在鼠標被拿走時消失。在Javascript字符串中使用Bootstrap的彈出式插件

<script type="text/javascript"> 
    $('#directions-panel').text("We can't give you directions to our office as you don't have", <a href="#" rel="popover" data-content="Some content..." data-original-title="Some title..." > geolocation </a> " enabled on your browser. Enable geolocation and try again. The map above will show you where our office is located."); 
</script> 

而且使用這樣的調用?

$("[rel=popover]") 
    .popover({ 
     offset: 10 
    }) 
    .mouseover(function(e) { 
     e.preventDefault() 
    }) 

我知道這是有點全地方,但我已經四處搜尋,找不到任何類似於我想做的事情。我確信它可以完成,任何人都指向正確的方向?

編輯:

我知道有聯繫頁面如下:

<div id="directions-panel"> 
     <div id="directions-error-message" style="visibility:hidden"> 
      We can't give you directions to our office as you don't have <a href="#" id="geoloc-popover" rel="popover" data-content="Some content..." data-original-title="Some title..." > geolocation </a> enabled on your browser. Enable geolocation and try again. The map above will show you where our office is located. 
     </div> 
</div> 

和DIV directions-error-message變得可見,當下面的JS function failure()火災:

function failure() { 
     alert("Your browser does not have geolocation enabled so we can't give you directions to our office. Enable geolocation and try again, or consult the map for our address."); 
     $("#directions-error-message").css({ 
      visibility: 'visible' 
     });  
     var destMapOptions = { 
      zoom:15, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      center: new google.maps.LatLng(ourLocation.lat, ourLocation.lng) 
     } 
     map = new google.maps.Map(document.getElementById("map-canvas"), destMapOptions); 
     marker = new google.maps.Marker({ 
      position: ourLatLng, 
      map: map, 
      draggable: false, 
      animation: google.maps.Animation.DROP, 
      title: "Hello" 
     }); 
     google.maps.event.addListener(marker, 'click', toggleBounce); 
    } 


    $(function() { 
     $('#directions-error-message').popover({ 
      selector: '[rel="popover"]', 
      trigger: 'hover' 
     }); 
    }) 

是否有辦法只需在directions-error-message DIV觸發彈出窗口中的「地理定位」?

+0

現在你所要做的就是將'$('#directions-error-message')'改爲'$('#geoloc-popover')'。您正在使用ID#directions-error-message而不是僅鏈接將彈出式觸發器設置爲整個div。 –

+0

仍似乎沒有工作。我把觸發器放在'失敗'裏面,但仍然沒有區別。我想通過ID選擇錨點?那會不會像'$('a [id =「#geoloc-popover」]')'? – MattSull

+0

您不希望失敗功能中的觸發器。而且任何標籤的id都可以用'#theidvalue'引用,所以你不需要''[id =「#geoloc-popover」]'。嘗試從彈出功能中刪除選擇器行。應該是'$(function(){$('#geoloc-popover')。popover({trigger:'hover'});})'。 –

回答

1

爲什麼不嘗試這樣的事情,而不是:

<div style="visibility:hidden"> 
    We can't give you directions to our office as you don't have", <a href="#" rel="popover" data-content="Some content..." data-original-title="Some title..." > geolocation </a> " enabled on your browser. Enable geolocation and try again. The map above will show you where our office is located. 
</div> 

然後換用JavaScript的div的可見性。這樣jQuery設置標題就可以識別鏈接,但用戶直到你想要它時纔會看到它。

+0

謝謝,這似乎是一個更好的解決方案,我嘗試了它,但它的工作原理,但我想popover切換鼠標懸停,就像@merv下面建議的方式。 – MattSull

+0

「彈出」的默認動作懸停,所以我不確定你的意思。這聽起來像你的問題,你有問題,它激活彈出窗口,因爲HTML頁面繪製時不存在。也許我讀錯了,但你能澄清問題是什麼嗎? –

+0

請參閱編輯的問題 – MattSull

1

Twitter Bootstrap's Popover plugin確實有一個選項來啓用其data-api來觸發切換彈出。啓用它將使所有動態添加的元素具有正確的標記工作,而無需JavaScript中的進一步初始化調用。

該功能在默認情況主要是因爲訂閱所有頁面上的的mouseenter鼠標離開事件可能會造成性能問題(這是在TBS < 2.1默認)禁用。如果您可以保持激活區域的相對特定性,您應該表現良好。

要啓用它只是爲#directions-panel上懸停,語法是:

$('#directions-panel').popover({selector: '[rel="popover"]', trigger: 'hover'}); 

所有後續更新,其中包括popovers那麼將是默認的活動面板。

+0

我從字面上只有一件事情,我想要一個彈出窗口,所以性能不應該是一個問題,但感謝您指出任何方式。我只想要包含地理位置的錨標記(如原始問題所示)觸發彈出窗口。是否有可能去'$('#directions-error-message a')。popover ...'? – MattSull

+1

如果在添加錨點後調用它,那麼這將起作用,但選擇加入數據api的整個過程是這樣的,以至於ajaxy的東西不需要後續調用。如果你知道你只是將它應用到一個錨標籤,你可以使用:'$('#directions-panel')。popover({selector:'a [rel =「popover」]')'。彈出窗口對象必須附加到頁面上的持久對象上。 'selector'值是指定元素的值。只要你不在其他錨上添加'rel =「popover」',那麼它們將被忽略。 – merv