以下工作代碼從頭部獲取所有meta標籤,並在網頁的div內顯示文本。在keyup中,我想複製輸入文本val並替換頭元描述,並更新div,以便我可以看到文本從前端正確輸入。on keyup,更新元描述
http://jsfiddle.net/michelm/JCWgA/
二是我有問題的東西:
- 複製文本框中輸入的新值,替換的META NAME =「說明」內容的「內容」 =」新文本增加了」(在div內)
- 複製輸入文本框的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>
http://jsfiddle.net/JCWgA/1/ – softsdev
woaw :)你真棒! – Michel
@softsdev爲什麼不只是回答這個問題? (所以它沒有出現在「未答覆」部分) – tborychowski