2013-11-04 83 views
0

要更改指定的標籤任意打開文件,我試圖再次保存該文件。html文件打開並保存jquery標籤替換

對不起,我使用了翻譯 jquery標籤如何取代...?

的test.html↓

<banner_0101> 
     <div class="banner"><a href="#" rel="external"><img src="test.jpg" ></a></div> 
</banner_0101> 

<banner_0127> 
    <div class="banner"><img src="http://test.jpg" ></div> 
</banner_0127> 

test.php的↓

<div id="form"> 
    <form action="testok.php" method="POST"> 
     <div> 
      <label for="cate">CATE_NUMBER</label> 
      <input type="text" name="cate" id="cate"> 
     </div> 
     <div> 
      <label for="href">A href </label> 
      <input type="text" name="href" id="href"> 
     </div> 
     <div> 
      <label for="imgsrc">Img src</label> 
      <input type="text" name="imgsrc" id="imgsrc"> 
     </div> 
     <div> 
      <input type="submit" value="OK"> 
     </div> 
    </form> 
</div> 

ex) 
POST Value 
test.ok -> 
$_POST['cate'] -> banner_0101 
$_POST['href'] -> /test1.php 
$_POST['imgsrc'] -> testok.jpg 

test.html ↓ 

<banner_0101> 
     <div class="banner"><a href="/test1.php" rel="external"><img src="testok.jpg" ></a></div> 
</banner_0101> 

**How do I resolve this problem?** 

回答

0

之一,是許多解決方案(尋找特定的旗幟和更換使用.find().replaceWith())可以是這樣的DOM而不去PHP和只是在jQuery中添加此腳本中:

$('#form form').on('submit', function(e){ 

    e.preventDefault(); //Stops your form default behavior 

    banner = $('#cate').val(); //banner value 
    link = $('#href').val(); //link 
    imgsrc = $('#imgsrc').val(); //img src 

    newDom = $('<'+ banner +'>').html($('<div>' , {'class' : 'banner'}).append($('<a>' ,{'href' : link }).append($('<img>',{'src': imgsrc })))); //your new HTML 

    $('body').find(banner).replaceWith(newDom); //find the specific banner and replace existing banner with new DOM 


});