2013-08-29 91 views
0

以下工作代碼從頭部獲取所有meta標籤,並在網頁的div內顯示文本。在keyup中,我想複製輸入文本val並替換頭元描述,並更新div,以便我可以看到文本從前端正確輸入。on keyup,更新元描述

http://jsfiddle.net/michelm/JCWgA/

二是我有問題的東西:

  1. 複製文本框中輸入的新值,替換的META NAME =「說明」內容的「內容」 =」新文本增加了」(在div內)
  2. 複製輸入文本框的VAL和更換頭部meta描述

的‘內容’部分代碼b的elow:

<h2>Meta Description</h2> 
    <!-- I have head meta tags inside the below div to read the tags on the web page --> 
    <div class="all">some text</div> 

<!-- input text on keyup to 1. update head meta description, 2 update meta description inside the div (<div class="all">some text</div>) --> 

<input type="text" name="metadescription" id="metadescription" /> 

<!-- this part of jquery is working --> 
<script type="text/javascript"> 

var foo = ''; 

$("head meta").each(function() { 
    foo += $(this).clone().wrap('<div>').parent().html(); 
}); 

//alert(foo); 
var b = $('.all').text(foo); 


// the below is part is not quite working 
$description = $('.all').find('meta[name=description]').attr('content'); 

$('#metadescription').keyup(function(){ 
    // $('.all').find($description).text($(this).val()); 
    //the below just replace all the text, but I want it to replace the 'content' 
    $('.all').text($(this).val()); 
    //alert(foo); 



}); 

</script> 

我得到這個工作,很大一部分去感謝的大部分代碼:))

*以下是完整的工作代碼的softsdev !!!! **

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<meta name="description" content="This is the meta description of some sort" > 
<meta name="keywords" content="some, content, added" > 
<title>change meta content</title> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('.content').keyup(function(){ 
      $('meta[name=description]').attr('content', $(this).val()); 
     }); 


    }); 

</script> 
</head> 

<body> 



    <h2>Meta Description</h2> 

<div class="all">some text</div> 
    <h4>Update meta description</h4> 
<input type="text" name="content" class="content"/> 

<script type="text/javascript"> 
$('.content').keyup(function(){ 

    $('.all').text('<meta name="description" content="'+$(this).val()+'">'); 

}); 


$('.all').append(($('meta[name=descritption]').attr('content'))); 
var tt = jQuery.metadata.get(name=descritption) 
    $('.all').text(tt); 
    $('meta[name=description]').attr('description').insertAfter('body'); 
</script> 

<script language="JavaScript"> 
if (document.getElementsByName) { 

    var metaArray = document.getElementsByName('description'); 
    for (var i=0; i<metaArray.length; i++) { 
    // document.write(metaArray[i].content + '<br>'); 
      $('.all').text('<meta name="description" content="'+ metaArray[i].content +'">'); 

    } 

} 
</script> 

</body> 
</html> 
+2

http://jsfiddle.net/JCWgA/1/ – softsdev

+0

woaw :)你真棒! – Michel

+1

@softsdev爲什麼不只是回答這個問題? (所以它沒有出現在「未答覆」部分) – tborychowski

回答

1

你可以像example

變化

$('.all').text($(this).val()); 

$('.all').text('<meta http-equiv="content-type" content="'+$(this).val()+'">'); 

更新回答

,如果你想這樣做在你的HTML,那麼你可以試試這個併爲您在螢火內容將改變meta標籤

,但它僅反映在客戶端

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<meta name="description" content="This is the meta description of some sort" > 
<meta name="keywords" content="some, content, added" > 
<title>change meta content</title> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('.content').keyup(function(){ 
      $('meta[name=description]').attr('content', $(this).val()); 
     }); 
    }); 

</script> 
</head> 

<body> 
    <input type="text" name="content" class="content"/> 
</body> 
</html> 
+0

你知道我該如何將這個值附加到頭部?我試過:$('head')。append(''); – Michel

+0

不錯的一個隊友,我添加了更多的代碼,只針對元描述,這是一個守護者! – Michel