2016-10-25 141 views
-2

當使用jQuery的.load選擇從另一個頁面像如何獲得jQuery的.load()從結果中排除div元素?

<div id="hello"> Hello </div> 

我如何得到它排除在結果div標籤一個div?

所以它只是返回「你好」。

更具體地講什麼,我實際上做的是越來越從一個網頁在它的一些HTML一個div並把它放在另一個。

<script> 
    $(document).ready(function(){ 
     $('.backup').on('click',function(e){ 
      var id = $(this).attr('id'); 
      var file_name = "../ajax/"+{{ json_encode($data[0]) }} + " #" + id + ".text"; 
      console.log(file_name); 
      //this is .load it takes in a string with a URL and a DOM element as a string. 
      // it will take the dom element found at the URL and place it in .page_text 
      $(".page_text").load(file_name); 
     }); 
    }); 
</script> 
+1

什麼負載,什麼的div,來自哪裏?沒有代碼? – adeneo

+1

無論如何,在編輯它之前,使用'$ .get'並獲取像'$(data).find('#hello')。text()' – adeneo

+0

使用恰當的語法 – Mahi

回答

1

$("#hello").text()給出了div的內容。只需使用文本方法。

+0

yes但是.load的語法類似'$('#element')。load(「url_to_look_in #id_you_want_to_get」);'所以我不太確定如何使用.text方法。 – IdecEddy

+0

你改變了這個問題。這個問題可以幫助你。從文件加載http://stackoverflow.com/questions/6470567/jquery-load-txt-file-and-insert-into-div – selami

0
$('#hello').text('Hello') 

這應該爲你做。

0

如果我理解正確,您正在尋找div的內容。

只使用JavaScript的

var getDiv = document.getElementById('hello').innerHTML //Hello 
0

如果你想選擇一個div

$( 「#你好」) - >這給了DIV與打招呼的ID

,如果你想得到它

$( 「#你好」)的內容。文本()將返回你好

0

使用jQuery爲您提:

$('hello').text() 

或使用Javascript:

document.getElementById('hello').innerHTML 

這將調用元素中只有文字與hello ID

1

有可能是沒有真正的方法做這個。

.load()將加載資源並將其插入到DOM中。該方法沒有參數來排除某些元素。

如果你絕對不能進入你的頁面的DOM的某些元素,你可能必須分析你裝上服務器端的資源和刪除元素存在。

但是,如果你可以得到加載的要素生活,你只是不希望他們看到,你可以做以下之一:

  • 負荷一切,然後delete元素都不想

  • 負載的一切,並使用CSS來隱藏所有你不想要的元素。因此,舉例來說,如果你的一切加載到<div>命名爲mystuff,您可以使用CSS來隱藏的div:

    #mystuff div { display: none; } 
    
+0

有沒有像'.load'的東西,但我可以將它設置爲一個變量在該元素上使用.text並將其推入我的文本框? – IdecEddy

+0

不確定你的意思 - 你的意思是加載資源爲文本?這應該是可能的與其他人建議(加載它,然後做一個'.text()') –

0

這將返回文本和HTML內容

$('#hello').html() 

這將返回div單獨的文字內容

$('#hello').text() 

實施例

$('.a').click(function(){ 
 

 
    alert($('.html').html()); 
 
}); 
 
    
 
$('.b').click(function(){ 
 

 
    alert($('.text').text()); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button class='a'> Get HTML </button> 
 

 
<button class='b'> Get Text</button> 
 
    
 
<div class='html'> 
 
<p> This is the HTML plus Content </p> 
 
</div> 
 
<div class='text'> 
 
    <p> This is the Text Content only </p> 
 
    </div>

+0

爲什麼在這個問題的每個人都認爲OP想要div的文本內容?他們只是想排除某些元素,不是嗎? –