

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jQuery &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Wed, 22 May 2024 02:17:42 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>jQuery &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>jQuery 搬移區塊</title>
		<link>https://stackoverflow.max-everyday.com/2024/04/jquery-move-div/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/04/jquery-move-div/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 12 Apr 2024 11:03:26 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5644</guid>

					<description><![CDATA[這個技能滿實用的, 有時候想移動div 到特定位...]]></description>
										<content:encoded><![CDATA[
<p>這個技能滿實用的, 有時候想移動div 到特定位置, 或是固定搬到最後一個, </p>



<pre class="wp-block-code"><code>$(this).insertAfter(target_div);</code></pre>



<p>解法: How to move an element after another element using JS or jquery?<br><a href="https://stackoverflow.com/questions/14549125/how-to-move-an-element-after-another-element-using-js-or-jquery">https://stackoverflow.com/questions/14549125/how-to-move-an-element-after-another-element-using-js-or-jquery</a></p>



<p>舉例:</p>



<p>I would like to move one&nbsp;<code>DIV</code>&nbsp;element beside another, it is normally like this:</p>



<pre class="wp-block-code"><code>&lt;div class="box-content-top"&gt;
 &lt;div class="box-related-product-top"&gt;
  &lt;div&gt;  

    &lt;div class="price"&gt;
      ...   
    &lt;/div&gt;              
    &lt;div class="image"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="name"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="cart"&gt;
     ...
    &lt;/div&gt;


   &lt;div&gt;  

    &lt;div class="price"&gt;
      ...   
    &lt;/div&gt;            
    &lt;div class="image"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="name"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="cart"&gt;
     ...
    &lt;/div&gt;

  &lt;/div&gt;
&lt;/div&gt;
</code></pre>



<p>I want to change the position of the&nbsp;<code>div</code>&nbsp;with the class&nbsp;<code>.price</code>&nbsp;to be after the&nbsp;<code>.name</code>&nbsp;class, to look like this:</p>



<pre class="wp-block-code"><code>&lt;div class="box-content-top"&gt;
 &lt;div class="box-related-product-top"&gt;
  &lt;div&gt;  

    &lt;div class="image"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="name"&gt;
     ...
    &lt;/div&gt;
     &lt;div class="price"&gt; // HERE
      ...   
    &lt;/div&gt; 
    &lt;div class="cart"&gt;
     ...
    &lt;/div&gt;


   &lt;div&gt;  

    &lt;div class="image"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="name"&gt;
     ...
    &lt;/div&gt;
    &lt;div class="price"&gt; // HERE
      ...   
    &lt;/div&gt; 
    &lt;div class="cart"&gt;
     ...
    &lt;/div&gt;

  &lt;/div&gt;
&lt;/div&gt;</code></pre>



<p>解法:</p>



<p>You can use&nbsp;<code>insertAfter</code>&nbsp;to move the element.&nbsp;<a href="http://api.jquery.com/insertAfter/">Docs</a></p>



<pre class="wp-block-code"><code>$('.price').each(function() {
    $(this).insertAfter($(this).parent().find('.name'));
});</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>這個解法有一個限制，就是在搬家的時候，無法把自己的孫子節點搬成自己小孩節點，會顯得示錯誤訊息：</p>



<pre class="wp-block-code"><code>jquery-3.6.3.min.js:2 Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/04/jquery-move-div/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>jQuery 如何存取控制 iframe 中的element</title>
		<link>https://stackoverflow.max-everyday.com/2024/01/jquery-iframe-element/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/01/jquery-iframe-element/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 02 Jan 2024 10:41:35 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5306</guid>

					<description><![CDATA[如果要存取 iframe 中的元素的話，可以使用...]]></description>
										<content:encoded><![CDATA[
<p>如果要存取 iframe 中的元素的話，可以使用 .contents() 後再來 .find().</p>



<p>範例程式:</p>



<pre class="wp-block-code"><code>$("iframe.punch-present-iframe").each(function() {
    var $iframe = $(this), 
        $contents = $iframe.contents();
        $font_style_block = $contents.find('#MaxChangeFontCss');
        if($font_style_block.length==0) {
            // task-1
        } else {
            // task-2
        }
});</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/01/jquery-iframe-element/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Adding options to a select using jQuery</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/adding-options-to-a-using-jquery/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/adding-options-to-a-using-jquery/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 22 Feb 2023 07:51:08 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4380</guid>

					<description><![CDATA[之前有試過動態加 &#60;div&#62; 到某個...]]></description>
										<content:encoded><![CDATA[
<p>之前有試過動態加 &lt;div&gt; 到某個節點: Creating a div element in jQuery<br><a href="https://stackoverflow.max-everyday.com/2023/02/creating-a-div-element-in-jquery/">https://stackoverflow.max-everyday.com/2023/02/creating-a-div-element-in-jquery/</a></p>



<p>今天要學的是把 array 裡的值, 變成 &lt;select&gt; 裡的 option.<br><a href="https://stackoverflow.com/questions/740195/adding-options-to-a-select-using-jquery">https://stackoverflow.com/questions/740195/adding-options-to-a-select-using-jquery</a></p>



<p>Personally, I prefer this syntax for appending options:</p>



<pre class="wp-block-code"><code>$('#mySelect').append($('&lt;option&gt;', {
    value: 1,
    text: 'My option'
}));
</code></pre>



<p>If you&#8217;re adding options from a collection of items, you can do the following:</p>



<pre class="wp-block-code"><code>$.each(items, function (i, item) {
    $('#mySelect').append($('&lt;option&gt;', { 
        value: item.value,
        text : item.text 
    }));
});</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<pre class="wp-block-code"><code>$.each(selectValues, function(key, value) {   
     $('#mySelect')
         .append($("&lt;option&gt;&lt;/option&gt;")
                    .attr("value", key)
                    .text(value)); 
});</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/adding-options-to-a-using-jquery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>jQuery 判斷 checkbox 是否選取</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/jquery-checkbox-checked/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/jquery-checkbox-checked/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 22 Feb 2023 02:22:08 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4368</guid>

					<description><![CDATA[前一個登入的表單, 有一個神奇的小勾勾, 叫 r...]]></description>
										<content:encoded><![CDATA[
<p>前一個登入的表單, 有一個神奇的小勾勾, 叫 remember me, </p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="564" height="512" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-22-10_14_34-Signin-Template-·-Bootstrap-v5.3.jpg" alt="" class="wp-image-4371"/></figure>



<p></p>



<p>要實作這個功能, 要先能讀到 checkbox 是否有勾,  jquery 語法:</p>



<pre class="wp-block-code"><code> if($("#clickAll").prop("checked")) {...}</code></pre>



<p>要做到這個功能, 還需要對  cookie 做存取, 提供二個簡單的 function:</p>



<pre class="wp-block-code"><code>function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i &lt;ca.length; i++) {
    let c = ca&#91;i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function setCookie(cname, cvalue, exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}</code></pre>



<p>與前一篇文章加起來, 完整的範例就是:</p>



<pre class="wp-block-code"><code>  let do_signin_flag = true;
  $("#fail-message").html("");
  if($("#account").val().length==0){
    $("#fail-message").html("Account is required.");
    $("#account").focus();
    do_signin_flag=false;
  } else {
    if($("#remember-me").prop("checked")) {
      setCookie("account",$("#account").val());
    }
  }
</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">相關文章</h2>



<p>jquery check textbox empty<br><a href="https://stackoverflow.max-everyday.com/2023/02/jquery-check-textbox-empty/">https://stackoverflow.max-everyday.com/2023/02/jquery-check-textbox-empty/</a></p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/jquery-checkbox-checked/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>jquery check textbox empty</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/jquery-check-textbox-empty/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/jquery-check-textbox-empty/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 22 Feb 2023 02:09:53 +0000</pubDate>
				<category><![CDATA[css筆記]]></category>
		<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[Bootstrap]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4366</guid>

					<description><![CDATA[我有一個登入用的 form, 用 bootstr...]]></description>
										<content:encoded><![CDATA[
<p>我有一個登入用的 form, 用 bootstrap 做的, 希望可以檢查 account 欄位是否有填值.</p>



<pre class="wp-block-code"><code>if (inp.val().length &gt; 0) {
    // do something
}
</code></pre>



<p>if you want anything more complicated, consider regex or use the&nbsp;<a href="http://docs.jquery.com/Plugins/Validation">validation plugin</a>&nbsp;which takes care of this for you.</p>



<p>在檢查到登入失敗, 或無填值, 有二種處理方式, 一個是彈 modal dialog, 一個是用 danger text, 如果不是太重大的提醒, 應該使用 danger text, 使用方法:</p>



<pre class="wp-block-code"><code>&lt;div class="text-danger" id='fail-message'&gt;your message here.&lt;/div&gt;</code></pre>



<p>我的登入 script:</p>



<pre class="wp-block-code"><code>  let do_signin_flag = true;
  $("#fail-message").html("");
  if($("#account").val().length==0){
    $("#fail-message").html("Account is required.");
    $("#account").focus();
    do_signin_flag=false;
  }</code></pre>



<p></p>



<p>我使用的範例:<br><a href="https://getbootstrap.com/docs/5.3/examples/sign-in/">https://getbootstrap.com/docs/5.3/examples/sign-in/</a></p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="564" height="512" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-22-10_14_34-Signin-Template-·-Bootstrap-v5.3.jpg" alt="" class="wp-image-4371"/></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>How to Set the Value of Input Text Box using jQuery</p>



<p>範例:</p>



<pre class="wp-block-code"><code>&lt;script>
$(document).ready(function(){
    $("#myInput").val("https://");
});
&lt;/script></code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/jquery-check-textbox-empty/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Creating a div element in jQuery</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/creating-a-div-element-in-jquery/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/creating-a-div-element-in-jquery/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 14 Feb 2023 02:02:28 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jsplumb]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4329</guid>

					<description><![CDATA[想在 jsplumb 裡動態新增視窗, 原來只需...]]></description>
										<content:encoded><![CDATA[
<p>想在 jsplumb 裡動態新增視窗, 原來只需要新建一個 div node, 指定 classname 與 id 後, append 進去 parent node 就可以了. </p>



<p>You can use&nbsp;<code>append</code>&nbsp;(to add at last position of parent) or&nbsp;<code>prepend</code>&nbsp;(to add at fist position of parent):</p>



<pre class="wp-block-code"><code>$('#parent').append('&lt;div&gt;hello&lt;/div&gt;');    
// or
$('&lt;div&gt;hello&lt;/div&gt;').appendTo('#parent');
</code></pre>



<p>Alternatively, you can use the&nbsp;<code>.html()</code>&nbsp;or&nbsp;<code>.add()</code>&nbsp;as mentioned.</p>



<pre class="wp-block-code"><code>div = $("&lt;div&gt;").html("Loading......");
$("body").prepend(div);    </code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Technically&nbsp;<code>$('&lt;div&gt;&lt;/div&gt;')</code>&nbsp;will &#8216;create&#8217; a&nbsp;<code>div</code>&nbsp;element (or more specifically a DIV DOM element) but won&#8217;t add it to your HTML document. You will then need to use that in combination with the other answers to actually do anything useful with it (such as using the&nbsp;<code>append()</code>&nbsp;method or such like).</p>



<p>The&nbsp;<a href="http://docs.jquery.com/Manipulation">manipulation documentation</a>&nbsp;gives you all the various options on how to add new elements.</p>



<pre class="wp-block-code"><code>d = document.createElement('div');
$(d).addClass(classname)
    .html(text)
    .appendTo($("#myDiv"))
.click(function () {
    $(this).remove();
})
);</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Example 2:<br><a href="https://stackoverflow.com/questions/867916/creating-a-div-element-in-jquery">https://stackoverflow.com/questions/867916/creating-a-div-element-in-jquery</a></p>



<p><strong><br>If you are using Jquery &gt; 1.4, you are best of with&nbsp;<a href="https://stackoverflow.com/a/4158203/3025256">Ian&#8217;s answer</a>. Otherwise, I would use this method:</strong></p>



<p>This is very similar to&nbsp;<a href="https://stackoverflow.com/a/6976126/3025256">celoron&#8217;s answer</a>, but I don&#8217;t know why they used&nbsp;<code>document.createElement</code>&nbsp;instead of Jquery notation.</p>



<pre class="wp-block-code"><code>$("body").append(function(){
        return $("&lt;div/&gt;").html("I'm a freshly created div. I also contain some Ps!")
            .attr("id","myDivId")
            .addClass("myDivClass")
            .css("border", "solid")                 
            .append($("&lt;p/&gt;").html("I think, therefore I am."))
            .append($("&lt;p/&gt;").html("The die is cast."))
});

//Some style, for better demonstration if you want to try it out. Don't use this approach for actual design and layout!
$("body").append($("&lt;style/&gt;").html("p{background-color:blue;}div{background-color:yellow;}div&gt;p{color:white;}"));
</code></pre>



<p>I also think using&nbsp;<code>append()</code>&nbsp;with a callback function is in this case more readable, because you now immediately that something is going to be appended to the body. But that is a matter of taste, as always when writing any code or text.</p>



<p>In general, use as less HTML as possible in JQuery code, since this is mostly spaghetti code. It is error prone and hard to maintain, because the HTML-String can easily contain typos. Also, it mixes a markup language (HTML) with a programming language (Javascript/Jquery), which is usually a bad Idea.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/creating-a-div-element-in-jquery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>jquery add class remove class</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/jquery-add-class-remove-class/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/jquery-add-class-remove-class/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 13 Feb 2023 06:40:16 +0000</pubDate>
				<category><![CDATA[css筆記]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4315</guid>

					<description><![CDATA[jQuery has several metho...]]></description>
										<content:encoded><![CDATA[
<p>jQuery has several methods for CSS manipulation. We will look at the following methods:</p>



<ul class="wp-block-list">
<li><code>addClass()</code>&nbsp;&#8211; Adds one or more classes to the selected elements</li>



<li><code>removeClass()</code>&nbsp;&#8211; Removes one or more classes from the selected elements</li>



<li><code>toggleClass()</code>&nbsp;&#8211; Toggles between adding/removing classes from the selected elements</li>



<li><code>css()</code>&nbsp;&#8211; Sets or returns the style attribute</li>
</ul>



<p></p>



<p>新增 class 範例:</p>



<p>$(&#8220;button&#8221;).click(function(){<br>  $(&#8220;div&#8221;).addClass(&#8220;important&#8221;);<br>});</p>



<p></p>



<p>移除 class 範例:</p>



<p>$(&#8220;button&#8221;).click(function(){<br>  $(&#8220;div&#8221;).removeClass(&#8220;blue&#8221;);<br>});</p>



<p></p>



<p>比較常見的是用切換某一個屬性, 可以用 toggle</p>



<p>$(&#8220;button&#8221;).click(function(){<br>  $(&#8220;h1, h2, p&#8221;).toggleClass(&#8220;blue&#8221;);<br>});</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/jquery-add-class-remove-class/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
